Skip to content

Support native Date type, Components, improved RegExp, new CustomValue API #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions shells/dev/target/Date.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<template>
<div id="date">
<p>Date: {{ date.toString() }} - Hours: {{ hours }} - Prototype: {{ date | prototypeString }}</p>

<p>
<button @click="updateDate">Update Date</button>
</p>
</div>
</template>

<script>
import { mapState, mapGetters, mapMutations } from 'vuex'

export default {
data () {
return {
localDate: new Date()
}
},
computed: {
...mapState(['date']),
...mapGetters(['hours'])
},
methods: {
...mapMutations({
updateDate: 'UPDATE_DATE'
})
},
filters: {
prototypeString: val => Object.prototype.toString.call(val)
}
}
</script>
2 changes: 2 additions & 0 deletions shells/dev/target/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import store from './store'
import Target from './Target.vue'
import Other from './Other.vue'
import Counter from './Counter.vue'
import Date from './Date.vue'
import Events from './Events.vue'
import MyClass from './MyClass.js'

Expand All @@ -19,6 +20,7 @@ new Vue({
render (h) {
return h('div', null, [
h(Counter),
h(Date),
h(Target, {props:{msg: 'hi', ins: new MyClass()}}),
h(Other),
h(Events)
Expand Down
11 changes: 8 additions & 3 deletions shells/dev/target/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ Vue.use(Vuex)

export default new Vuex.Store({
state: {
count: 0
count: 0,
date: new Date()
},
mutations: {
INCREMENT: state => state.count++,
DECREMENT: state => state.count--
DECREMENT: state => state.count--,
UPDATE_DATE: state => {
state.date = new Date()
}
},
getters: {
isPositive: state => state.count >= 0
isPositive: state => state.count >= 0,
hours: state => state.date.getHours()
}
})
10 changes: 9 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export function stringify (data) {
return CircularJSON.stringify(data, replacer)
}

function replacer (key, val) {
function replacer (key) {
const val = this[key]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yyx990803 This is to prevent the default replacer to turn Dates into Strings before calling the custom replacer. See WebReflection/circular-json#4

if (val === undefined) {
return UNDEFINED
} else if (val === Infinity) {
Expand All @@ -53,6 +54,8 @@ function replacer (key, val) {
} else if (val instanceof RegExp) {
// special handling of native type
return `[native RegExp ${val.toString()}]`
} else if (val instanceof Date) {
return `[native Date ${val.toString()}]`
} else {
return sanitize(val)
}
Expand All @@ -64,13 +67,18 @@ export function parse (data, revive) {
: CircularJSON.parse(data)
}

const specialTypeRE = /^\[native (\w+) (.*)\]$/

function reviver (key, val) {
if (val === UNDEFINED) {
return undefined
} else if (val === INFINITY) {
return Infinity
} else if (val === NAN) {
return NaN
} else if (specialTypeRE.test(val)) {
const [, type, string] = specialTypeRE.exec(val)
return new window[type](string)
} else {
return val
}
Expand Down