Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ejson

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ejson - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

test.js

4

build.js

@@ -26,7 +26,7 @@ 'use strict';

//
'var EJSON, EJSONTest, i, base64Encode, base64Decode, root = {};',
'var EJSON, EJSONTest, i, Base64, root = {};',
'var _ = require("underscore");',
fs.readFileSync(path.join(__dirname, './vendor/base64.js'), 'utf-8'),
fs.readFileSync(path.join(__dirname, './vendor/ejson.js'), 'utf-8'),
fs.readFileSync(path.join(__dirname, './vendor/base64.js'), 'utf-8'),

@@ -33,0 +33,0 @@ ' return EJSON;',

module.exports = (function () {
"use strict";
var Meteor = { _noYieldsAllowed:function nope(f) { return f(); }};
var EJSON, EJSONTest, i, base64Encode, base64Decode, root = {};
var EJSON, EJSONTest, i, Base64, root = {};
var _ = require("underscore");
// Base 64 encoding
var BASE_64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var BASE_64_VALS = {};
for (var i = 0; i < BASE_64_CHARS.length; i++) {
BASE_64_VALS[BASE_64_CHARS.charAt(i)] = i;
};
Base64 = {};
Base64.encode = function (array) {
if (typeof array === "string") {
var str = array;
array = Base64.newBinary(str.length);
for (var i = 0; i < str.length; i++) {
var ch = str.charCodeAt(i);
if (ch > 0xFF) {
throw new Error(
"Not ascii. Base64.encode can only take ascii strings.");
}
array[i] = ch;
}
}
var answer = [];
var a = null;
var b = null;
var c = null;
var d = null;
for (var i = 0; i < array.length; i++) {
switch (i % 3) {
case 0:
a = (array[i] >> 2) & 0x3F;
b = (array[i] & 0x03) << 4;
break;
case 1:
b = b | (array[i] >> 4) & 0xF;
c = (array[i] & 0xF) << 2;
break;
case 2:
c = c | (array[i] >> 6) & 0x03;
d = array[i] & 0x3F;
answer.push(getChar(a));
answer.push(getChar(b));
answer.push(getChar(c));
answer.push(getChar(d));
a = null;
b = null;
c = null;
d = null;
break;
}
}
if (a != null) {
answer.push(getChar(a));
answer.push(getChar(b));
if (c == null)
answer.push('=');
else
answer.push(getChar(c));
if (d == null)
answer.push('=');
}
return answer.join("");
};
var getChar = function (val) {
return BASE_64_CHARS.charAt(val);
};
var getVal = function (ch) {
if (ch === '=') {
return -1;
}
return BASE_64_VALS[ch];
};
// XXX This is a weird place for this to live, but it's used both by
// this package and 'ejson', and we can't put it in 'ejson' without
// introducing a circular dependency. It should probably be in its own
// package or as a helper in a package that both 'base64' and 'ejson'
// use.
Base64.newBinary = function (len) {
if (typeof Uint8Array === 'undefined' || typeof ArrayBuffer === 'undefined') {
var ret = [];
for (var i = 0; i < len; i++) {
ret.push(0);
}
ret.$Uint8ArrayPolyfill = true;
return ret;
}
return new Uint8Array(new ArrayBuffer(len));
};
Base64.decode = function (str) {
var len = Math.floor((str.length*3)/4);
if (str.charAt(str.length - 1) == '=') {
len--;
if (str.charAt(str.length - 2) == '=')
len--;
}
var arr = Base64.newBinary(len);
var one = null;
var two = null;
var three = null;
var j = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
var v = getVal(c);
switch (i % 4) {
case 0:
if (v < 0)
throw new Error('invalid base64 string');
one = v << 2;
break;
case 1:
if (v < 0)
throw new Error('invalid base64 string');
one = one | (v >> 4);
arr[j++] = one;
two = (v & 0x0F) << 4;
break;
case 2:
if (v >= 0) {
two = two | (v >> 2);
arr[j++] = two;
three = (v & 0x03) << 6;
}
break;
case 3:
if (v >= 0) {
arr[j++] = three | v;
}
break;
}
}
return arr;
};
/**
* @namespace
* @summary Namespace for EJSON functions
*/
EJSON = {};
EJSONTest = {};
// Custom type interface definition
/**
* @class CustomType
* @instanceName customType
* @memberOf EJSON
* @summary The interface that a class must satisfy to be able to become an
* EJSON custom type via EJSON.addType.
*/
/**
* @function typeName
* @memberOf EJSON.CustomType
* @summary Return the tag used to identify this type. This must match the tag used to register this type with [`EJSON.addType`](#ejson_add_type).
* @locus Anywhere
* @instance
*/
/**
* @function toJSONValue
* @memberOf EJSON.CustomType
* @summary Serialize this instance into a JSON-compatible value.
* @locus Anywhere
* @instance
*/
/**
* @function clone
* @memberOf EJSON.CustomType
* @summary Return a value `r` such that `this.equals(r)` is true, and modifications to `r` do not affect `this` and vice versa.
* @locus Anywhere
* @instance
*/
/**
* @function equals
* @memberOf EJSON.CustomType
* @summary Return `true` if `other` has a value equal to `this`; `false` otherwise.
* @locus Anywhere
* @param {Object} other Another object to compare this to.
* @instance
*/
var customTypes = {};

@@ -22,3 +216,8 @@ // Add a custom type, using a method of your choice to get to and

// but you may provide a method equals() instead.
//
/**
* @summary Add a custom datatype to EJSON.
* @locus Anywhere
* @param {String} name A tag for your custom type; must be unique among custom data types defined in your project, and must match the result of your type's `typeName` method.
* @param {Function} factory A function that deserializes a JSON-compatible value into an instance of your type. This should match the serialization performed by your type's `toJSONValue` method.
*/
EJSON.addType = function (name, factory) {

@@ -78,6 +277,6 @@ if (_.has(customTypes, name))

toJSONValue: function (obj) {
return {$binary: base64Encode(obj)};
return {$binary: Base64.encode(obj)};
},
fromJSONValue: function (obj) {
return base64Decode(obj.$binary);
return Base64.decode(obj.$binary);
}

@@ -189,2 +388,7 @@ },

/**
* @summary Serialize an EJSON-compatible value into its plain JSON representation.
* @locus Anywhere
* @param {EJSON} val A value to serialize to plain JSON.
*/
EJSON.toJSONValue = function (item) {

@@ -254,2 +458,7 @@ var changed = toJSONValueHelper(item);

/**
* @summary Deserialize an EJSON value from its plain JSON representation.
* @locus Anywhere
* @param {JSONCompatible} val A value to deserialize into EJSON.
*/
EJSON.fromJSONValue = function (item) {

@@ -266,2 +475,12 @@ var changed = fromJSONValueHelper(item);

/**
* @summary Serialize a value to a string.
For EJSON values, the serialization fully represents the value. For non-EJSON values, serializes the same way as `JSON.stringify`.
* @locus Anywhere
* @param {EJSON} val A value to stringify.
* @param {Object} [options]
* @param {Boolean | Integer | String} options.indent Indents objects and arrays for easy readability. When `true`, indents by 2 spaces; when an integer, indents by that number of spaces; and when a string, uses the string as the indentation pattern.
* @param {Boolean} options.canonical When `true`, stringifies keys in an object in sorted order.
*/
EJSON.stringify = function (item, options) {

@@ -276,2 +495,7 @@ var json = EJSON.toJSONValue(item);

/**
* @summary Parse a string into an EJSON value. Throws an error if the string is not valid EJSON.
* @locus Anywhere
* @param {String} str A string to parse into an EJSON value.
*/
EJSON.parse = function (item) {

@@ -283,2 +507,7 @@ if (typeof item !== 'string')

/**
* @summary Returns true if `x` is a buffer of binary data, as returned from [`EJSON.newBinary`](#ejson_new_binary).
* @param {Object} x The variable to check.
* @locus Anywhere
*/
EJSON.isBinary = function (obj) {

@@ -289,2 +518,10 @@ return !!((typeof Uint8Array !== 'undefined' && obj instanceof Uint8Array) ||

/**
* @summary Return true if `a` and `b` are equal to each other. Return false otherwise. Uses the `equals` method on `a` if present, otherwise performs a deep comparison.
* @locus Anywhere
* @param {EJSON} a
* @param {EJSON} b
* @param {Object} [options]
* @param {Boolean} options.keyOrderSensitive Compare in key sensitive order, if supported by the JavaScript implementation. For example, `{a: 1, b: 2}` is equal to `{b: 2, a: 1}` only when `keyOrderSensitive` is `false`. The default is `false`.
*/
EJSON.equals = function (a, b, options) {

@@ -371,2 +608,7 @@ var i;

/**
* @summary Return a deep copy of `val`.
* @locus Anywhere
* @param {EJSON} val A value to copy.
*/
EJSON.clone = function (v) {

@@ -416,131 +658,15 @@ var ret;

// Base 64 encoding
/**
* @summary Allocate a new buffer of binary data that EJSON can serialize.
* @locus Anywhere
* @param {Number} size The number of bytes of binary data to allocate.
*/
// EJSON.newBinary is the public documented API for this functionality,
// but the implementation is in the 'base64' package to avoid
// introducing a circular dependency. (If the implementation were here,
// then 'base64' would have to use EJSON.newBinary, and 'ejson' would
// also have to use 'base64'.)
EJSON.newBinary = Base64.newBinary;
var BASE_64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var BASE_64_VALS = {};
for (var i = 0; i < BASE_64_CHARS.length; i++) {
BASE_64_VALS[BASE_64_CHARS.charAt(i)] = i;
};
base64Encode = function (array) {
var answer = [];
var a = null;
var b = null;
var c = null;
var d = null;
for (var i = 0; i < array.length; i++) {
switch (i % 3) {
case 0:
a = (array[i] >> 2) & 0x3F;
b = (array[i] & 0x03) << 4;
break;
case 1:
b = b | (array[i] >> 4) & 0xF;
c = (array[i] & 0xF) << 2;
break;
case 2:
c = c | (array[i] >> 6) & 0x03;
d = array[i] & 0x3F;
answer.push(getChar(a));
answer.push(getChar(b));
answer.push(getChar(c));
answer.push(getChar(d));
a = null;
b = null;
c = null;
d = null;
break;
}
}
if (a != null) {
answer.push(getChar(a));
answer.push(getChar(b));
if (c == null)
answer.push('=');
else
answer.push(getChar(c));
if (d == null)
answer.push('=');
}
return answer.join("");
};
var getChar = function (val) {
return BASE_64_CHARS.charAt(val);
};
var getVal = function (ch) {
if (ch === '=') {
return -1;
}
return BASE_64_VALS[ch];
};
EJSON.newBinary = function (len) {
if (typeof Uint8Array === 'undefined' || typeof ArrayBuffer === 'undefined') {
var ret = [];
for (var i = 0; i < len; i++) {
ret.push(0);
}
ret.$Uint8ArrayPolyfill = true;
return ret;
}
return new Uint8Array(new ArrayBuffer(len));
};
base64Decode = function (str) {
var len = Math.floor((str.length*3)/4);
if (str.charAt(str.length - 1) == '=') {
len--;
if (str.charAt(str.length - 2) == '=')
len--;
}
var arr = EJSON.newBinary(len);
var one = null;
var two = null;
var three = null;
var j = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
var v = getVal(c);
switch (i % 4) {
case 0:
if (v < 0)
throw new Error('invalid base64 string');
one = v << 2;
break;
case 1:
if (v < 0)
throw new Error('invalid base64 string');
one = one | (v >> 4);
arr[j++] = one;
two = (v & 0x0F) << 4;
break;
case 2:
if (v >= 0) {
two = two | (v >> 2);
arr[j++] = two;
three = (v & 0x03) << 6;
}
break;
case 3:
if (v >= 0) {
arr[j++] = three | v;
}
break;
}
}
return arr;
};
EJSONTest.base64Encode = base64Encode;
EJSONTest.base64Decode = base64Decode;
return EJSON;
}).call(this);
{
"name": "ejson",
"version": "1.0.1",
"version": "2.0.0",
"description": "EJSON - Extended and Extensible JSON library from Meteor made compatible for Nodejs and Browserif",

@@ -8,10 +8,11 @@ "main": "index.js",

"prepublish": "node build.js",
"test": "npm run prepublish && mocha"
"test": "npm run prepublish && mocha --check-leaks test.js"
},
"keywords": [
"EJSON",
"JSON",
"Extended",
"Extensible",
"JSON",
"Meteor",
"ejson",
"primus"

@@ -22,13 +23,13 @@ ],

"type": "git",
"url": "git://github.com/primus/EJSON.git"
"url": "git://github.com/primus/ejson.git"
},
"license": "MIT",
"dependencies": {
"underscore": "1.5.x"
"underscore": "1.7.x"
},
"devDependencies": {
"mocha": "1.12.x",
"chai": "1.7.x",
"assume": "0.0.x",
"mocha": "2.1.x",
"pre-commit": "0.0.x"
}
}
# ejson
[![Build Status](https://travis-ci.org/primus/EJSON.png)](https://travis-ci.org/primus/EJSON)
[![NPM version](https://badge.fury.io/js/ejson.png)](http://badge.fury.io/js/ejson)
[![Version npm](http://img.shields.io/npm/v/ejson.svg?style=flat-square)](http://browsenpm.org/package/ejson)[![Build Status](http://img.shields.io/travis/primus/EJSON/master.svg?style=flat-square)](https://travis-ci.org/primus/EJSON)[![Dependencies](https://img.shields.io/david/primus/EJSON.svg?style=flat-square)](https://david-dm.org/primus/EJSON)[![Coverage Status](http://img.shields.io/coveralls/primus/EJSON/master.svg?style=flat-square)](https://coveralls.io/r/primus/EJSON?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23primus-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=primus)

@@ -10,3 +9,3 @@ `ejson` is an extension of JSON to support more types. It supports all JSON-safe

- **Date**: It maintains a date instance instead of transforming it to a string.
- **Binary**: `Uinit8Array`
- **Binary**: `Uint8Array`
- **User defined types**

@@ -13,0 +12,0 @@

@@ -25,3 +25,3 @@ 'use strict';

//
'var EJSON, EJSONTest, i, base64Encode, base64Decode, root = {};',
'var EJSON, EJSONTest, i, Base64, root = {};',

@@ -34,4 +34,4 @@ //

'var _ = "undefined" !== typeof exports ? exports._ : root._;',
read(path.join(__dirname, './vendor/base64.js'), 'utf-8'),
read(path.join(__dirname, './vendor/ejson.js'), 'utf-8'),
read(path.join(__dirname, './vendor/base64.js'), 'utf-8'),

@@ -38,0 +38,0 @@ ' return EJSON;',

@@ -11,3 +11,19 @@ // Base 64 encoding

base64Encode = function (array) {
Base64 = {};
Base64.encode = function (array) {
if (typeof array === "string") {
var str = array;
array = Base64.newBinary(str.length);
for (var i = 0; i < str.length; i++) {
var ch = str.charCodeAt(i);
if (ch > 0xFF) {
throw new Error(
"Not ascii. Base64.encode can only take ascii strings.");
}
array[i] = ch;
}
}
var answer = [];

@@ -66,3 +82,8 @@ var a = null;

EJSON.newBinary = function (len) {
// XXX This is a weird place for this to live, but it's used both by
// this package and 'ejson', and we can't put it in 'ejson' without
// introducing a circular dependency. It should probably be in its own
// package or as a helper in a package that both 'base64' and 'ejson'
// use.
Base64.newBinary = function (len) {
if (typeof Uint8Array === 'undefined' || typeof ArrayBuffer === 'undefined') {

@@ -79,3 +100,3 @@ var ret = [];

base64Decode = function (str) {
Base64.decode = function (str) {
var len = Math.floor((str.length*3)/4);

@@ -87,3 +108,3 @@ if (str.charAt(str.length - 1) == '=') {

}
var arr = EJSON.newBinary(len);
var arr = Base64.newBinary(len);

@@ -128,5 +149,1 @@ var one = null;

};
EJSONTest.base64Encode = base64Encode;
EJSONTest.base64Decode = base64Decode;

@@ -0,4 +1,53 @@

/**
* @namespace
* @summary Namespace for EJSON functions
*/
EJSON = {};
EJSONTest = {};
// Custom type interface definition
/**
* @class CustomType
* @instanceName customType
* @memberOf EJSON
* @summary The interface that a class must satisfy to be able to become an
* EJSON custom type via EJSON.addType.
*/
/**
* @function typeName
* @memberOf EJSON.CustomType
* @summary Return the tag used to identify this type. This must match the tag used to register this type with [`EJSON.addType`](#ejson_add_type).
* @locus Anywhere
* @instance
*/
/**
* @function toJSONValue
* @memberOf EJSON.CustomType
* @summary Serialize this instance into a JSON-compatible value.
* @locus Anywhere
* @instance
*/
/**
* @function clone
* @memberOf EJSON.CustomType
* @summary Return a value `r` such that `this.equals(r)` is true, and modifications to `r` do not affect `this` and vice versa.
* @locus Anywhere
* @instance
*/
/**
* @function equals
* @memberOf EJSON.CustomType
* @summary Return `true` if `other` has a value equal to `this`; `false` otherwise.
* @locus Anywhere
* @param {Object} other Another object to compare this to.
* @instance
*/
var customTypes = {};

@@ -17,3 +66,8 @@ // Add a custom type, using a method of your choice to get to and

// but you may provide a method equals() instead.
//
/**
* @summary Add a custom datatype to EJSON.
* @locus Anywhere
* @param {String} name A tag for your custom type; must be unique among custom data types defined in your project, and must match the result of your type's `typeName` method.
* @param {Function} factory A function that deserializes a JSON-compatible value into an instance of your type. This should match the serialization performed by your type's `toJSONValue` method.
*/
EJSON.addType = function (name, factory) {

@@ -73,6 +127,6 @@ if (_.has(customTypes, name))

toJSONValue: function (obj) {
return {$binary: base64Encode(obj)};
return {$binary: Base64.encode(obj)};
},
fromJSONValue: function (obj) {
return base64Decode(obj.$binary);
return Base64.decode(obj.$binary);
}

@@ -184,2 +238,7 @@ },

/**
* @summary Serialize an EJSON-compatible value into its plain JSON representation.
* @locus Anywhere
* @param {EJSON} val A value to serialize to plain JSON.
*/
EJSON.toJSONValue = function (item) {

@@ -249,2 +308,7 @@ var changed = toJSONValueHelper(item);

/**
* @summary Deserialize an EJSON value from its plain JSON representation.
* @locus Anywhere
* @param {JSONCompatible} val A value to deserialize into EJSON.
*/
EJSON.fromJSONValue = function (item) {

@@ -261,2 +325,12 @@ var changed = fromJSONValueHelper(item);

/**
* @summary Serialize a value to a string.
For EJSON values, the serialization fully represents the value. For non-EJSON values, serializes the same way as `JSON.stringify`.
* @locus Anywhere
* @param {EJSON} val A value to stringify.
* @param {Object} [options]
* @param {Boolean | Integer | String} options.indent Indents objects and arrays for easy readability. When `true`, indents by 2 spaces; when an integer, indents by that number of spaces; and when a string, uses the string as the indentation pattern.
* @param {Boolean} options.canonical When `true`, stringifies keys in an object in sorted order.
*/
EJSON.stringify = function (item, options) {

@@ -271,2 +345,7 @@ var json = EJSON.toJSONValue(item);

/**
* @summary Parse a string into an EJSON value. Throws an error if the string is not valid EJSON.
* @locus Anywhere
* @param {String} str A string to parse into an EJSON value.
*/
EJSON.parse = function (item) {

@@ -278,2 +357,7 @@ if (typeof item !== 'string')

/**
* @summary Returns true if `x` is a buffer of binary data, as returned from [`EJSON.newBinary`](#ejson_new_binary).
* @param {Object} x The variable to check.
* @locus Anywhere
*/
EJSON.isBinary = function (obj) {

@@ -284,2 +368,10 @@ return !!((typeof Uint8Array !== 'undefined' && obj instanceof Uint8Array) ||

/**
* @summary Return true if `a` and `b` are equal to each other. Return false otherwise. Uses the `equals` method on `a` if present, otherwise performs a deep comparison.
* @locus Anywhere
* @param {EJSON} a
* @param {EJSON} b
* @param {Object} [options]
* @param {Boolean} options.keyOrderSensitive Compare in key sensitive order, if supported by the JavaScript implementation. For example, `{a: 1, b: 2}` is equal to `{b: 2, a: 1}` only when `keyOrderSensitive` is `false`. The default is `false`.
*/
EJSON.equals = function (a, b, options) {

@@ -366,2 +458,7 @@ var i;

/**
* @summary Return a deep copy of `val`.
* @locus Anywhere
* @param {EJSON} val A value to copy.
*/
EJSON.clone = function (v) {

@@ -410,1 +507,13 @@ var ret;

};
/**
* @summary Allocate a new buffer of binary data that EJSON can serialize.
* @locus Anywhere
* @param {Number} size The number of bytes of binary data to allocate.
*/
// EJSON.newBinary is the public documented API for this functionality,
// but the implementation is in the 'base64' package to avoid
// introducing a circular dependency. (If the implementation were here,
// then 'base64' would have to use EJSON.newBinary, and 'ejson' would
// also have to use 'base64'.)
EJSON.newBinary = Base64.newBinary;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc