Comparing version 0.0.5 to 0.1.0
38
array.js
// | ||
// map-to/array | ||
// as/array | ||
// ------------------------------------------------------------------------------------------------- | ||
// Maps a `{a: b}` object to an array of `{key: a, value: b}` pairs. | ||
// Maps an `{a: b}` object to a new array of `{key: a, value: b}` pairs. | ||
// | ||
/** | ||
* @param {Object} object – The object to be mapped | ||
* @returns {Array} – A new array of key-value pairs mapped from the object | ||
* @function asArray | ||
* | ||
* @param {Object} object | ||
* The object to be mapped. | ||
* | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* The depth to which the `object`'s tree should be mapped. Set it to `Infinity` to map the | ||
* entire tree structure. | ||
* | ||
* @returns {Array} | ||
* A new array of key-value pairs mapped from the object. | ||
*/ | ||
module.exports = function mapToArray (object) { | ||
var key; | ||
module.exports = function asArray (object, options, _depthLeft) { | ||
var key, value; | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
if (object instanceof Array) return object.slice(); | ||
var result = []; | ||
for (key in object) if (object.hasOwnProperty(key)) { | ||
result.push({key: key, value: object[key]}); | ||
value = object[key]; | ||
if ( _depthLeft | ||
&& value !== null && typeof value == "object" | ||
) { | ||
value = asArray(value, options, _depthLeft - 1); | ||
} | ||
result.push({key: key, value: value}); | ||
} | ||
@@ -16,0 +38,0 @@ |
@@ -0,7 +1,10 @@ | ||
// | ||
// Consider importing individual functions: | ||
// var mapToArray = require('map-to/array'); | ||
// | ||
// var asArray = require("as/array"); | ||
// | ||
module.exports = | ||
{ array: require('./array') | ||
, object: require('./object') | ||
{ array: require("./array") | ||
, object: require("./object") | ||
}; |
// | ||
// map-to/object | ||
// as/object | ||
// ------------------------------------------------------------------------------------------------- | ||
// Maps an array of `{key: a, value: b}` pairs to a `{a: b}` object. | ||
// Maps an array of `{key: a, value: b}` pairs to a new `{a: b}` object. | ||
// | ||
/** | ||
* @param {Object} object – The array of key-value pairs to be mapped. | ||
* @returns {Array} – A new object mapped from the array. | ||
* @function asObject | ||
* | ||
* @param {Array} array | ||
* The array of key-value pairs to be mapped. | ||
* | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* The depth to which the `array`'s pairs should be traversed. Set it to `Infinity` to map the | ||
* whole structure. | ||
* | ||
* @returns {Object} | ||
* A new object mapped from the array. | ||
*/ | ||
module.exports = function mapToObject (array) { | ||
var i, l, pair; | ||
module.exports = function asObject (array, options, _depthLeft) { | ||
var pair, value; | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
var result = {}; | ||
var i = 0; var l = array.length; while (i < l) { | ||
pair = array[i++]; | ||
if (!pair || !pair.hasOwnProperty("key")) continue; | ||
i = 0; l = array.length; while (i < l) { | ||
pair = array[i++]; | ||
if (!pair || !pair.hasOwnProperty('key')) continue; | ||
result[pair.key] = pair.value; | ||
value = pair.value; | ||
if (_depthLeft && value instanceof Array) { | ||
value = asObject(value, options, _depthLeft - 1); | ||
} | ||
result[pair.key] = value; | ||
} | ||
@@ -18,0 +37,0 @@ |
{ "name": "as" | ||
, "version": "0.0.5" | ||
, "version": "0.1.0" | ||
, "description": "as/array and as/object. Convert easily, back and forth." | ||
@@ -34,3 +34,3 @@ , "license": "MIT" | ||
, "scripts": | ||
{ "test": "node test | faucet" | ||
{ "test": "node test" | ||
, "coveralls": "istanbul cover test --report lcovonly && coveralls < ./coverage/lcov.info" | ||
@@ -40,3 +40,2 @@ } | ||
{ "tape": "3.0.3" | ||
, "faucet": "0.0.1" | ||
, "coveralls": "2.11.2" | ||
@@ -43,0 +42,0 @@ , "istanbul": "0.3.5" |
@@ -1,2 +0,2 @@ | ||
[![Build status](https://img.shields.io/travis/tomekwi/as.js.svg?style=flat-square)](https://img.shields.io/travis/tomekwi/as.js/master.svg) | ||
[![Build status](https://img.shields.io/travis/tomekwi/as.js/master.svg?style=flat-square)](https://travis-ci.org/tomekwi/as.js) | ||
[![Coveralls](https://img.shields.io/coveralls/tomekwi/as.js.svg?style=flat-square)](https://coveralls.io/r/tomekwi/as.js) | ||
@@ -3,0 +3,0 @@ [![Code climate](https://img.shields.io/codeclimate/github/tomekwi/as.js.svg?style=flat-square)](https://codeclimate.com/github/tomekwi/as.js) |
@@ -1,24 +0,86 @@ | ||
var test = require('tape'); | ||
var mapToArray = require('../array'); | ||
var test = require("tape"); | ||
var asArray = require("../array"); | ||
test('map-to/array', function (tape) { | ||
test("as/array", function (tape) { | ||
var deepEqual = Function.prototype.apply.bind(tape.deepEqual, null); | ||
[ [ mapToArray({}) | ||
// Basic functionality | ||
// ----------------------------------------------------------------------------------------------- | ||
[ [ asArray({a: "b", c: "d"}) | ||
, [ {key: "a", value: "b"} | ||
, {key: "c", value: "d"} | ||
] | ||
, "should do the job for a simple array" | ||
] | ||
, [ asArray({}) | ||
, [] | ||
, "should return `[]` for an empty object" | ||
] | ||
, [ mapToArray({a: 'b', c: 'd'}) | ||
, [ {key: 'a', value: 'b'} | ||
, {key: 'c', value: 'd'} | ||
, [ asArray( | ||
{ a: null | ||
, b: 0 | ||
, c: true | ||
, d: false | ||
, e: undefined | ||
, f: "string" | ||
, g: "" | ||
, h: ["array"] | ||
}) | ||
, [ {key: "a", value: null} | ||
, {key: "b", value: 0} | ||
, {key: "c", value: true} | ||
, {key: "d", value: false} | ||
, {key: "e", value: undefined} | ||
, {key: "f", value: "string"} | ||
, {key: "g", value: ""} | ||
, {key: "h", value: ["array"]} | ||
] | ||
, "should work for various data types" | ||
] | ||
, [ mapToArray({a: 'b', c: 'd', e: {f: 'g'}}) | ||
, [ {key: 'a', value: 'b'} | ||
, {key: 'c', value: 'd'} | ||
, {key: 'e', value: {f: 'g'}} | ||
// `options.depth` | ||
// ----------------------------------------------------------------------------------------------- | ||
, [ asArray({a: "b", c: "d", e: {f: "g"}}) | ||
, [ {key: "a", value: "b"} | ||
, {key: "c", value: "d"} | ||
, {key: "e", value: {f: "g"}} | ||
] | ||
, "should map shallowly by default" | ||
] | ||
, [ asArray({a: "b", c: "d", e: {f: "g"}}, {depth: 1}) | ||
, [ {key: "a", value: "b"} | ||
, {key: "c", value: "d"} | ||
, {key: "e", value: [ {key: "f", value: "g"} | ||
]} | ||
] | ||
, "should map one level deep" | ||
] | ||
, [ asArray({a: "b", c: "d", e: {f: "g", h: {i: "j"}}}, {depth: 1}) | ||
, [ {key: "a", value: "b"} | ||
, {key: "c", value: "d"} | ||
, {key: "e", value: [ {key: "f", value: "g"} | ||
, {key: "h", value: {i: "j"}} | ||
]} | ||
] | ||
, "should map only one level deep" | ||
] | ||
, [ asArray({a: "b", c: "d", e: {f: "g", h: {i: "j"}}}, {depth: Infinity}) | ||
, [ {key: "a", value: "b"} | ||
, {key: "c", value: "d"} | ||
, {key: "e", value: [ {key: "f", value: "g"} | ||
, {key: "h", value: [{key: "i", value: "j"}]} | ||
]} | ||
] | ||
, "should map deeply" | ||
] | ||
].map(deepEqual); | ||
@@ -25,0 +87,0 @@ |
@@ -1,2 +0,4 @@ | ||
require('./array'); | ||
require('./object'); | ||
require("./array"); | ||
require("./object"); | ||
require("./array-object"); |
@@ -1,30 +0,50 @@ | ||
var test = require('tape'); | ||
var mapToObject = require('../object'); | ||
var test = require("tape"); | ||
var asObject = require("../object"); | ||
test('map-to/object', function (tape) { | ||
test("as/object", function (tape) { | ||
var deepEqual = Function.prototype.apply.bind(tape.deepEqual, null); | ||
[ [ mapToObject([]) | ||
, {} | ||
] | ||
, [ mapToObject( | ||
[ {key: 'a', value: 'b'} | ||
, {key: 'c', value: 'd'} | ||
// Basic functionality | ||
// ----------------------------------------------------------------------------------------------- | ||
[ [ asObject( | ||
[ {key: "a", value: "b"} | ||
, {key: "c", value: "d"} | ||
]) | ||
, {a: 'b', c: 'd'} | ||
, {a: "b", c: "d"} | ||
, "should do the job for a simple array" | ||
] | ||
, [ mapToObject( | ||
[ {key: 'a', value: 'b'} | ||
, {key: 'c', value: 'd'} | ||
, {key: 'e', value: {key: 'f', value: 'g'}} | ||
, [ asObject([]) | ||
, {} | ||
, "should return `{}` for an empty array" | ||
] | ||
, [ asObject( | ||
[ {key: "a", value: null} | ||
, {key: "b", value: 0} | ||
, {key: "c", value: true} | ||
, {key: "d", value: false} | ||
, {key: "e", value: undefined} | ||
, {key: "f", value: "string"} | ||
, {key: "g", value: ""} | ||
, {key: "h", value: ["array"]} | ||
]) | ||
, {a: 'b', c: 'd', e: {key: 'f', value: 'g'}} | ||
, { a: null | ||
, b: 0 | ||
, c: true | ||
, d: false | ||
, e: undefined | ||
, f: "string" | ||
, g: "" | ||
, h: ["array"] | ||
} | ||
, "should work for various data types" | ||
] | ||
, [ mapToObject( | ||
[ {key: 'a', value: 'b'} | ||
, [ asObject( | ||
[ {key: "a", value: "b"} | ||
, null | ||
, {key: 'c', value: 'd'} | ||
, {key: "c", value: "d"} | ||
, undefined | ||
@@ -34,6 +54,56 @@ , {} | ||
, true | ||
, {another: "structure", value: "anything"} | ||
, NaN | ||
, 10 | ||
]) | ||
, {a: 'b', c: 'd'} | ||
, {a: "b", c: "d"} | ||
, "should ignore values which don't match the `{key, value}` structure" | ||
] | ||
// `options.depth` | ||
// ----------------------------------------------------------------------------------------------- | ||
, [ asObject( | ||
[ {key: "a", value: "b"} | ||
, {key: "c", value: [{key: "d", value: "e"}]} | ||
]) | ||
, {a: "b", c: [{key: "d", value: "e"}]} | ||
, "should map shallowly by default" | ||
] | ||
, [ asObject | ||
( [ {key: "a", value: "b"} | ||
, {key: "c", value: [{key: "d", value: "e"}]} | ||
] | ||
, {depth: 1} | ||
) | ||
, {a: "b", c: {d: "e"}} | ||
, "should map one level deep" | ||
] | ||
, [ asObject | ||
( [ {key: "a", value: "b"} | ||
, {key: "c", value: [ {key: "d", value: "e"} | ||
, {key: "f", value: [{key: "g", value: "h"}]} | ||
]} | ||
] | ||
, {depth: 1} | ||
) | ||
, {a: "b", c: {d: "e", f: [{key: "g", value: "h"}]}} | ||
, "should map only one level deep" | ||
] | ||
, [ asObject | ||
( [ {key: "a", value: "b"} | ||
, {key: "c", value: [ {key: "d", value: "e"} | ||
, {key: "f", value: [{key: "g", value: "h"}]} | ||
]} | ||
] | ||
, {depth: Infinity} | ||
) | ||
, {a: "b", c: {d: "e", f: {g: "h"}}} | ||
, "should map deeply" | ||
] | ||
].map(deepEqual); | ||
@@ -40,0 +110,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
12633
3
15
313
1