Comparing version 3.0.2 to 3.0.3
@@ -1,1 +0,1 @@ | ||
!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Typeson=t():r.Typeson=t()}(this,function(){return function(r){function t(n){if(e[n])return e[n].exports;var o=e[n]={exports:{},id:n,loaded:!1};return r[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var e={};return t.m=r,t.c=e,t.p="",t(0)}([function(r,t){function e(r){var t=[],e={},u=this.types={};this.stringify=function(r,t,e){return JSON.stringify(c(r),t,e)},this.parse=function(r,t){return f(JSON.parse(r,t))};var c=this.encapsulate=function(n){function i(r,t,e){var n=typeof t;if(n in{string:1,"boolean":1,number:1,undefined:1})return"number"===n&&(isNaN(t)||t===-(1/0)||t===1/0)?u(r,t):t;if(null==t)return t;if(e){var a=f.indexOf(t);if(!(0>a))return c[r]="#","#"+s[a];e===!0&&(f.push(t),s.push(r))}var p=t.constructor===Object?t:u(r,t);if(p!==t)return p;if(null==t)return t;var v;if(t.constructor===Object)v={};else{if(t.constructor!==Array)return t;v=new Array(t.length)}return o(t).forEach(function(n){var o=i(r+(r?".":"")+n,t[n],e);void 0!==o&&(v[n]=o)}),v}function u(r,n){for(var o=t.length;o--;)if(t[o].test(n)){var u=t[o].type;if(e[u]){var f=c[r];c[r]=f?[u].concat(f):u}return i(r,t[o].replace(n),"readonly")}return n}var c={},f=[],s=[],a=i("",n,r&&"cyclic"in r?r.cyclic:!0);if(o(c).length){if(a.constructor!==Object||a.$types)return{$:a,$types:{$:c}};a.$types=c}return a},f=this.revive=function(r){function t(r,f,s){if(!c||"$types"!==r){var a=u[r];if(f&&(f.constructor===Object||f.constructor===Array)){var p=i(f)?new Array(f.length):{};o(f).forEach(function(e){var n=t(r+(r?".":"")+e,f[e],s||p);void 0!==n&&(p[e]=n)}),f=p}return a?"#"===a?n(s,f.substr(1)):[].concat(a).reduce(function(r,t){var n=e[t];if(!n)throw new Error("Unregistered type: "+t);return n(r)},f):f}}var u=r.$types,c=!0;return u?(u.$&&u.$.constructor===Object&&(r=r.$,u=u.$,c=!1),t("",r)):r};this.register=function(r){return[].concat(r).forEach(function n(r){return i(r)?r.map(n):void(r&&o(r).forEach(function(n){var o=r[n],i=t.filter(function(r){return r.type===n});i.length&&(t.splice(t.indexOf(i[0]),1),delete e[n],delete u[n]),o&&(t.push({type:n,test:o[0],replace:o[1]}),o[2]&&(e[n]=o[2]),u[n]=o)}))}),this}}function n(r,t){if(""===t)return r;var e=t.indexOf(".");if(-1!==e){var o=r[t.substr(0,e)];return void 0===o?void 0:n(o,t.substr(e+1))}return r[t]}var o=Object.keys,i=Array.isArray;r.exports=e}])}); | ||
!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Typeson=t():r.Typeson=t()}(this,function(){return function(r){function t(n){if(e[n])return e[n].exports;var o=e[n]={exports:{},id:n,loaded:!1};return r[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var e={};return t.m=r,t.c=e,t.p="",t(0)}([function(r,t){function e(r){var t=[],e={},u=this.types={};this.stringify=function(r,t,e){return JSON.stringify(c(r),t,e)},this.parse=function(r,t){return f(JSON.parse(r,t))};var c=this.encapsulate=function(n){function i(r,t,e){var n=typeof t;if(n in{string:1,"boolean":1,number:1,undefined:1})return"number"===n&&(isNaN(t)||t===-(1/0)||t===1/0)?u(r,t):t;if(null==t)return t;if(e){var a=f.indexOf(t);if(!(0>a))return c[r]="#","#"+s[a];e===!0&&(f.push(t),s.push(r))}var p=t.constructor===Object?t:u(r,t);if(p!==t)return p;if(null==t)return t;var v;if(t.constructor===Object)v={};else{if(t.constructor!==Array)return t;v=new Array(t.length)}return o(t).forEach(function(n){var o=i(r+(r?".":"")+n,t[n],e);void 0!==o&&(v[n]=o)}),v}function u(r,n){for(var o=t.length;o--;)if(t[o].test(n)){var u=t[o].type;if(e[u]){var f=c[r];c[r]=f?[u].concat(f):u}return i(r,t[o].replace(n),a&&"readonly")}return n}var c={},f=[],s=[],a=r&&"cyclic"in r?r.cyclic:!0,p=i("",n,a);if(o(c).length){if(p.constructor!==Object||p.$types)return{$:p,$types:{$:c}};p.$types=c}return p},f=this.revive=function(r){function t(r,f,s){if(!c||"$types"!==r){var a=u[r];if(f&&(f.constructor===Object||f.constructor===Array)){var p=i(f)?new Array(f.length):{};o(f).forEach(function(e){var n=t(r+(r?".":"")+e,f[e],s||p);void 0!==n&&(p[e]=n)}),f=p}return a?"#"===a?n(s,f.substr(1)):[].concat(a).reduce(function(r,t){var n=e[t];if(!n)throw new Error("Unregistered type: "+t);return n(r)},f):f}}var u=r.$types,c=!0;return u?(u.$&&u.$.constructor===Object&&(r=r.$,u=u.$,c=!1),t("",r)):r};this.register=function(r){return[].concat(r).forEach(function n(r){return i(r)?r.map(n):void(r&&o(r).forEach(function(n){var o=r[n],i=t.filter(function(r){return r.type===n});i.length&&(t.splice(t.indexOf(i[0]),1),delete e[n],delete u[n]),o&&(t.push({type:n,test:o[0],replace:o[1]}),o[2]&&(e[n]=o[2]),u[n]=o)}))}),this}}function n(r,t){if(""===t)return r;var e=t.indexOf(".");if(-1!==e){var o=r[t.substr(0,e)];return void 0===o?void 0:n(o,t.substr(e+1))}return r[t]}var o=Object.keys,i=Array.isArray;r.exports=e}])}); |
{ | ||
"name": "typeson", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "Preserves types over JSON, BSON or socket.io", | ||
@@ -5,0 +5,0 @@ "main": "typeson.js", |
@@ -14,3 +14,9 @@ # typeson.js | ||
``` | ||
NOTE: Typeson by itself wont support these types. Register the types you need: | ||
# Why? | ||
JSON can only contain strings, numbers, booleans, arrays and objects. If you want to serialize other types over HTTP, WebSocket, postMessage() or other channel, this module makes it possible to serialize any type over channels that normally only accepts vanilla objects. Typeson adds a metadata property "$types" to the result that maps each non-trivial property to a type name. The type name is a reference to a registered type specification that you need to have the same on both the stringifying and the parsing side. | ||
# Type Registry | ||
[typeson-registry](https://github.com/dfahlander/typeson-registry) contains encapsulation rules for standard javascript types such as Date, Error, ArrayBuffer, etc. Pick the types you need, use a preset or write your own. | ||
```js | ||
@@ -30,10 +36,5 @@ var typeson = new Typeson().register([ | ||
``` | ||
If you really need to support all built-in types, the module `typeson-registry/presets/builtin` is still **just 1.6 kb minizied and gzipped** and adds support 32 builtin javascript types: *Date, RegExp, NaN, Infinity, -Infinity, Set, Map, ArrayBuffer, DataView, Uint8Array, Int8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, Error, SyntaxError, TypeError, RangeError, ReferenceError, EvalError, URIError, InternalError, Intl.Collator, Intl.DateTimeFormat, Intl.NumberFormat, Object String, Object Number and Object Boolean*. | ||
The module `typeson-registry/presets/builtin` is 1.6 kb minizied and gzipped and adds support 32 builtin javascript types: *Date, RegExp, NaN, Infinity, -Infinity, Set, Map, ArrayBuffer, DataView, Uint8Array, Int8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, Error, SyntaxError, TypeError, RangeError, ReferenceError, EvalError, URIError, InternalError, Intl.Collator, Intl.DateTimeFormat, Intl.NumberFormat, Object String, Object Number and Object Boolean*. | ||
# Why? | ||
JSON can only contain strings, numbers, booleans, arrays and objects. If you want to serialize other types over HTTP, WebSocket, postMessage() or other channel, this module makes it possible to serialize any type over channels that normally only accepts vanilla objects. Typeson adds a metadata property "$types" to the result that maps each non-trivial property to a type name. The type name is a reference to a registered type specification that you need to have the same on both the stringifying and the parsing side. | ||
# Type Registry | ||
[typeson-registry](https://github.com/dfahlander/typeson-registry) contains encapsulation rules for standard javascript types such as Date, Error, ArrayBuffer, etc. Pick the types you need, use a preset or write your own. | ||
# Compatibility | ||
@@ -40,0 +41,0 @@ * Node |
@@ -53,3 +53,4 @@ var keys = Object.keys, | ||
// Clone the object deeply while at the same time replacing any special types or cyclic reference: | ||
var ret = _encapsulate ('', obj, options && ('cyclic' in options) ? options.cyclic : true); | ||
var cyclic = options && ('cyclic' in options) ? options.cyclic : true; | ||
var ret = _encapsulate ('', obj, cyclic); | ||
// Add $types to result only if we ever bumped into a special type | ||
@@ -120,3 +121,3 @@ if (keys(types).length) { | ||
// Now, also traverse the result in case it contains it own types to replace | ||
return _encapsulate(key, replacers[i].replace(value), "readonly"); | ||
return _encapsulate(key, replacers[i].replace(value), cyclic && "readonly"); | ||
} | ||
@@ -123,0 +124,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
454
278
35040