* [jsfm] refactor the normalize method
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/85e32df7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/85e32df7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/85e32df7 Branch: refs/heads/0.16-dev Commit: 85e32df780183e2517c15fcdaa42e82ef939fc32 Parents: 3fb8d99 Author: Hanks <zhanghan...@gmail.com> Authored: Wed Aug 2 14:13:27 2017 +0800 Committer: Hanks <zhanghan...@gmail.com> Committed: Wed Aug 2 14:13:27 2017 +0800 ---------------------------------------------------------------------- html5/runtime/buffer.js | 13 --------- html5/runtime/normalize.js | 48 ++++++++++++++++++++++++++++++ html5/runtime/task-center.js | 61 +++++++++------------------------------ 3 files changed, 62 insertions(+), 60 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/85e32df7/html5/runtime/buffer.js ---------------------------------------------------------------------- diff --git a/html5/runtime/buffer.js b/html5/runtime/buffer.js deleted file mode 100644 index ff610d5..0000000 --- a/html5/runtime/buffer.js +++ /dev/null @@ -1,13 +0,0 @@ -export function bufferToString (buffer) { - return String.fromCharCode.apply(null, new Uint16Array(buffer)) -} - -export function stringToBuffer (string) { - const N = string.length - const buffer = new ArrayBuffer(N * 2) - const view = new Uint16Array(buffer) - for (let i = 0; i < N; i++) { - view[i] = string.charCodeAt(i) - } - return buffer -} http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/85e32df7/html5/runtime/normalize.js ---------------------------------------------------------------------- diff --git a/html5/runtime/normalize.js b/html5/runtime/normalize.js new file mode 100644 index 0000000..3d9ad0b --- /dev/null +++ b/html5/runtime/normalize.js @@ -0,0 +1,48 @@ +export function typof (v) { + const s = Object.prototype.toString.call(v) + return s.substring(8, s.length - 1) +} + +/** + * Normalize a primitive value. + * @param {any} v + * @return {primitive} + */ +export function normalizePrimitive (v) { + const type = typof(v) + + switch (type) { + case 'Undefined': + case 'Null': + return '' + + case 'RegExp': + return v.toString() + case 'Date': + return v.toISOString() + + case 'Number': + case 'String': + case 'Boolean': + case 'Array': + case 'Object': + return v + + case 'ArrayBuffer': + return { type, buffer: v } + + case 'Int8Array': + case 'Uint8Array': + case 'Uint8ClampedArray': + case 'Int16Array': + case 'Uint16Array': + case 'Int32Array': + case 'Uint32Array': + case 'Float32Array': + case 'Float64Array': + return { type, buffer: v.buffer } + + default: + return JSON.stringify(v) + } +} http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/85e32df7/html5/runtime/task-center.js ---------------------------------------------------------------------- diff --git a/html5/runtime/task-center.js b/html5/runtime/task-center.js index f2fbcb5..b2f960f 100644 --- a/html5/runtime/task-center.js +++ b/html5/runtime/task-center.js @@ -18,7 +18,7 @@ */ import CallbackManager from './callback-manager' import Element from './vdom/element' -import { bufferToString } from './buffer' +import { typof, normalizePrimitive } from './normalize' let fallback = function () {} @@ -44,61 +44,28 @@ export class TaskCenter { return this.callbackManager.close() } - typof (v) { - const s = Object.prototype.toString.call(v) - return s.substring(8, s.length - 1) - } - /** * Normalize a value. Specially, if the value is a function, then generate a function id * and save it to `CallbackManager`, at last return the function id. * @param {any} v - * @param {object} app * @return {primitive} */ normalize (v) { - const type = this.typof(v) + const type = typof(v) - switch (type) { - case 'Undefined': - case 'Null': - return '' - case 'RegExp': - return v.toString() - case 'Date': - return v.toISOString() - case 'Number': - case 'String': - case 'Boolean': - case 'Array': - case 'Object': - if (v instanceof Element) { - return v.ref - } - if (v._isVue && v.$el instanceof Element) { - return v.$el.ref - } - return v - - case 'ArrayBuffer': - return { type, buffer: v, string: bufferToString(v) } - case 'Int8Array': - case 'Uint8Array': - case 'Uint8ClampedArray': - case 'Int16Array': - case 'Uint16Array': - case 'Int32Array': - case 'Uint32Array': - case 'Float32Array': - case 'Float64Array': - return { type, buffer: v.buffer, string: bufferToString(v.buffer) } - - case 'Function': - return this.callbackManager.add(v).toString() - /* istanbul ignore next */ - default: - return JSON.stringify(v) + if (v instanceof Element) { + return v.ref } + + if (v._isVue && v.$el instanceof Element) { + return v.$el.ref + } + + if (type === 'Function') { + return this.callbackManager.add(v).toString() + } + + return normalizePrimitive(v) } send (type, params, args, options) {