Socket
Socket
Sign inDemoInstall

jsprim

Package Overview
Dependencies
3
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.5.1 to 0.6.0

36

lib/jsprim.js

@@ -23,2 +23,3 @@ /*

exports.flattenObject = flattenObject;
exports.flattenIter = flattenIter;
exports.validateJsonObject = validateJsonObjectJS;

@@ -148,2 +149,37 @@ exports.validateJsonObjectJS = validateJsonObjectJS;

/*
* Invoke callback(row) for each entry in the array that would be returned by
* flattenObject(data, depth). This is just like flattenObject(data,
* depth).forEach(callback), except that the intermediate array is never
* created.
*/
function flattenIter(data, depth, callback)
{
doFlattenIter(data, depth, [], callback);
}
function doFlattenIter(data, depth, accum, callback)
{
var each;
var key;
if (depth === 0) {
each = accum.slice(0);
each.push(data);
callback(each);
return;
}
mod_assert.ok(data !== null);
mod_assert.equal(typeof (data), 'object');
mod_assert.equal(typeof (depth), 'number');
mod_assert.ok(depth >= 0);
for (key in data) {
each = accum.slice(0);
each.push(key);
doFlattenIter(data[key], depth - 1, each, callback);
}
}
function flattenObject(data, depth)

@@ -150,0 +186,0 @@ {

2

package.json
{
"name": "jsprim",
"version": "0.5.1",
"version": "0.6.0",
"description": "utilities for primitive JavaScript types",

@@ -5,0 +5,0 @@ "main": "./lib/jsprim.js",

@@ -86,2 +86,16 @@ # jsprim: utilities for primitive JavaScript types

### flattenIter(obj, depth, func)
This is similar to `flattenObject` except that instead of returning an array,
this function invokes `func(entry)` for each `entry` in the array that
`flattenObject` would return. `flattenIter(obj, depth, func)` is logically
equivalent to `flattenObject(obj, depth).forEach(func)`. Importantly, this
version never constructs the full array. Its memory usage is O(depth) rather
than O(n) (where `n` is the number of flattened elements).
There's another difference between `flattenObject` and `flattenIter` that's
related to the special case where `depth === 0`. In this case, `flattenObject`
omits the array wrapping `obj` (which is regrettable).
### pluck(obj, key)

@@ -88,0 +102,0 @@

@@ -169,2 +169,59 @@ /*

/* flattenIter */
v = {
'level1-A': {
'level2-Aa': {
'level3-Aai': 4,
'level3-Aaii': 7,
'level3-Aaiii': 2
},
'level2-Ab': {
'level3-Abi': 51,
'level3-Abii': 31
},
'level2-Ac': {
'level3-Aci': 1351,
'level3-Acii': 121
}
},
'level1-B': {
'level2-Ba': {
'level3-Bai': 8,
'level3-Baii': 7,
'level3-Baiii': 6
},
'level2-Bb': {
'level3-Bbi': 5,
'level3-Bbii': 4
},
'level2-Bc': {
'level3-Bci': 3,
'level3-Bcii': 2
}
}
};
var accum, unflattened;
[
[ v, 1 ],
[ v, 2 ],
[ v, 3 ]
].forEach(function (testcase, j) {
var flattened;
accum = [];
flattened = jsprim.flattenObject(testcase[0], testcase[1]);
jsprim.flattenIter(testcase[0], testcase[1],
function (entry) { accum.push(entry); });
console.error('test case %d', j, accum);
mod_assert.deepEqual(accum, flattened);
});
/*
* It was arguably a mistake the way flatten with depth === 0 works. That's the
* only case where the return value is not an array of arrays. flattenIter()
* does the more sensible thing here.
*/
accum = [];
jsprim.flattenIter(3, 0, function (entry) { accum.push(entry); });
mod_assert.deepEqual(accum, [ [ 3 ] ]);
/* pluck */

@@ -213,2 +270,2 @@ mod_assert.equal('hello', jsprim.pluck({ 'world': 'hello' }, 'world'));

console.log('tests okay');
console.log('basic tests okay');

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc