Socket
Socket
Sign inDemoInstall

typewise

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typewise - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

ses.js

2

context.js

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

// Intantiate a new SES context
vm.runInNewContext(source, copy, 'initSes.js');
vm.runInNewContext(source, copy, 'initSES.js');
return {

@@ -19,0 +19,0 @@ Function: copy.Function

{
"name": "typewise",
"version": "0.5.0",
"version": "0.6.0",
"description": "Typewise structured sorting for arbirarily complex data structures",
"main": "typewise.js",
"dependencies": {
"es6-shim": "~0.8"
"es6-shim": "~0.8",
"bops": "~0.0"
},
"optionalDependencies": {
"ses": "*",
"ses": "~0.0",
"esprima": "~1",
"escodegen": "*",
"buffertools": "*"
"escodegen": "~0.0",
"buffertools": "~1"
},
"browser": {
"./ses": false
},
"devDependencies": {
"tap": "*"
"tape": "~1.0.4"
},

@@ -35,3 +39,19 @@ "scripts": {

"license": "MIT",
"readmeFilename": "README.md"
"readmeFilename": "README.md",
"testling": {
"files": "test/*.js",
"browsers": [
"ie/8..latest",
"firefox/17..latest",
"firefox/nightly",
"chrome/22..latest",
"chrome/canary",
"opera/12..latest",
"opera/next",
"safari/5.1..latest",
"ipad/6.0..latest",
"iphone/6.0..latest",
"android-browser/4.2..latest"
]
}
}

@@ -21,4 +21,44 @@ typewise

## Future
### Generic collections
The ordering chosen for some of the types is somewhat arbitrary. It is intentionally structured to support those sorts defined by CouchDB and IndexedDB but there might be more logical placements, specifically for BUFFER, SET, and FUNCTION, which aren't defined in either. It may be beneficial to fully characterize the distinctions between collections that affect collation.
One possible breakdown for collection types:
* unordered set (order unimportant and thus sorted using standard collation)
* unordered multiset, duplicates allowed
* chain (ordered set) (order-preserving with distinct values)
* ordered multiset, duplicates allowed (a list)
* unordered map (keys as unordered set, objects are string-keyed maps)
* unordered multimap (keys as unordered multiset), duplicates allowed
* ordered map (keys as ordered set)
* ordered multimap (keys as ordered multiset), duplicates allowed
Perhaps we should always allow duplicates, and have the prevention of duplicates be a enforced at runtime by a schema of some sort.
The primary distinction between collections are whether their items are unary (sets or arrays of elements) or binary (maps of keys and values). The secondary distinction is whether the collection preserves the order of its elements or not. For instance, arrays preserve the order of their elements while sets do not. Maps typically don't either, nor do javascript objects (even if they appear to at first). These are the two bits which characterize collection types that globally effect the sorting of the types.
There is a third characterizing bit: whether or not duplicates are allowed. The effect this has on sort is very localized, only for breaking ties between two otherwise identical keys -- otherwise records are completely interwoven when sorted, regardless of whether duplicates are allowed or not.
We may want unique symbols to signal these characterizing bits for serialization.
We probably want hooks for custom revivers.
Sparse arrays could be modeled with sorted maps of integer keys, but should we use a trailer to preserve this data?
This is very close to a generalized total order for all possible data structure models.
### Performance
Encoding and decoding is surely slower than the native `JSON` functions, but there is plenty of room to narrow the gap. Once the serialization stabilizes a C port should be straitforward to narrow the gap further.
### Streams
Where this serialization should really shine is streaming. Building a stream from individually encoded elements should require little more than strait concatenation, and parsing a stream would be the same as parsing an array. Parsing is a little more complex than msgpack and many other binary encodings because we have to use termination characters, not length specifiers, for certain types to keep from screwing up our collation invariants. This also means we have to do a little escapement dance, which costs a little extra too.
## License
[MIT](http://deanlandolt.mit-license.org/)

@@ -5,9 +5,22 @@ 'use strict';

var util = require('./util');
var tape = require('tape')
var sample = util.getSample();
var shuffled = util.shuffle(sample.slice());
typewise.equal(sample, shuffled.sort(typewise.compare));
tape('simple', function (t) {
try {
var sample = util.getSample();
var shuffled = util.shuffle(sample.slice());
typewise.equal(sample, shuffled.sort(typewise.compare));
t.deepEqual(sample, shuffled.sort(typewise.compare))
var sample = util.getArraySample(2);
var shuffled = util.shuffle(sample.slice());
typewise.equal(sample, shuffled.sort(typewise.compare));
t.deepEqual(sample, shuffled.sort(typewise.compare))
console.log(sample)
} catch (err) {
t.notOk(err)
console.log(err.stack || err)
}
var sample = util.getArraySample(2);
var shuffled = util.shuffle(sample.slice());
typewise.equal(sample, shuffled.sort(typewise.compare));
t.end()
})
'use strict';
var util = exports;
var bops = require('bops')

@@ -53,11 +54,11 @@ var samples = {

buffer: [
new Buffer([]),
new Buffer([ 0 ]),
new Buffer([ 0, 0 ]),
new Buffer([ 0, 1 ]),
new Buffer([ 1, 0 ]),
new Buffer([ 1, 1 ]),
new Buffer([ 255 ]),
new Buffer([ 255, 0 ]),
new Buffer([ 255, 255 ])
bops.create([]),
bops.create([ 0 ]),
bops.create([ 0, 0 ]),
bops.create([ 0, 1 ]),
bops.create([ 1, 0 ]),
bops.create([ 1, 1 ]),
bops.create([ 255 ]),
bops.create([ 255, 0 ]),
bops.create([ 255, 255 ])
],

@@ -90,2 +91,4 @@ string: [

//Fisher–Yates shuffle
util.shuffle = function(o) {

@@ -107,3 +110,3 @@ for (var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x);

var sample = util.getSample();
sample.pop(); // pull off undefined
sample.pop(); // pop off undefined
if (!depth) return sample;

@@ -110,0 +113,0 @@ sample.forEach(function(item) {

'use strict';
require('es6-shim');
var bops = require('bops');
var typewise = exports;

@@ -57,3 +59,2 @@

typewise.equal = function(a, b) {
// TODO should this be variadic?
// TODO stringify functions, ignore prototypes, etc.

@@ -66,2 +67,11 @@ return assert.deepEqual(a, b);

function bytewiseCompare(a, b) {
var result;
for (var i = 0, length = Math.min(a.length, b.length); i < length; i++) {
result = bops.readUInt8(a, i) - bops.readUInt8(b, i);
if (result) return result;
}
return a.length - b.length;
}
var comparators = typewise.comparators = {

@@ -74,10 +84,3 @@ difference: function(a, b) {

},
bytewise: function(a, b) {
var result;
for (var i = 0, length = Math.min(a.length, b.length); i < length; i++) {
result = a.get(i) - b.get(i);
if (result) return result;
}
return a.length - b.length;
},
bytewise: bytewiseCompare,
elementwise: function(a, b) {

@@ -92,11 +95,13 @@ var result;

};
// FIXME Y U NO WORK BUFFERTOOLS?!
// Attempt to use the fast native version in buffertools
// try {
// require('buffertools').compare;
// comparators.bytewise = function(a, b) {
// return a.compare(b);
// }
// }
// catch (e) {}
try {
require('buffertools').compare;
comparators.bytewise = function(a, b) {
// Bypass buffertools compare if lengths differ
if (a.length !== b.length) return bytewiseCompare(a, b);
return a.compare(b);
}
}
catch (e) {}

@@ -147,6 +152,3 @@

binary: {
is: function(source) {
// TODO typed arrays, etc.
return source instanceof Buffer;
},
is: bops.is,
compare: comparators.bytewise

@@ -216,27 +218,3 @@ },

// Attempt to load necessary dependencies to parse and properly revive functions in a safe SES sandbox
try {
var esprima = require('esprima');
var escodegen = require('escodegen');
var context = require('./context');
types.function.serialize = function(value) {
var syntax = esprima.parse('(' + value + ')');
// TODO validate AST is a FunctionExpression in a ExpressionStatement
var params = syntax.body[0].expression.params.map(function(param) {
// TODO is this guard necessary?
if (param.type === 'Identifier') return params.name;
}).filter(function(param) {
return param;
});
// TODO play around with some escodegen options
// We could minify to normalize functions as best as possible
return params.concat(escodegen.generate(syntax.body[0].expression.body));
};
types.function.parse = function(syntax) {
return context.Function.apply(null, syntax);
};
}
catch (e) {
// TODO should we bother with fallbacks?
}
if(process.title != 'browser')
require('./ses')(types)
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