Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
Conflicts:
	package.json
  • Loading branch information
Tomek Wiszniewski committed Jan 5, 2015
2 parents b5c7092 + 36850d9 commit e5d1d90
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 56 deletions.
38 changes: 30 additions & 8 deletions array.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
//
// 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;
var result = [];
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});
}

return result;
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,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")
};
39 changes: 29 additions & 10 deletions object.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
//
// 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;
var result = {};
module.exports = function asObject (array, options, _depthLeft) {
var pair, value;

if (!options) options = {};
if (_depthLeft === void null && options.depth) _depthLeft = options.depth;

i = 0; l = array.length; while (i < l) {
var result = {};
var i = 0; var l = array.length; while (i < l) {
pair = array[i++];
if (!pair || !pair.hasOwnProperty('key')) continue;
result[pair.key] = pair.value;
if (!pair || !pair.hasOwnProperty("key")) continue;

value = pair.value;
if (_depthLeft && value instanceof Array) {
value = asObject(value, options, _depthLeft - 1);
}
result[pair.key] = value;
}

return result;
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ "name": "as"
, "version": "0.0.5"
, "version": "0.1.0"
, "description": "as/array and as/object. Convert easily, back and forth."
, "license": "MIT"
, "keywords":
Expand Down Expand Up @@ -31,12 +31,11 @@
}

, "scripts":
{ "test": "node test | faucet"
{ "test": "node test"
, "coveralls": "istanbul cover test --report lcovonly && coveralls < ./coverage/lcov.info"
}
, "devDependencies":
{ "tape": "3.0.3"
, "faucet": "0.0.1"
, "coveralls": "2.11.2"
, "istanbul": "0.3.5"
}
Expand Down
70 changes: 70 additions & 0 deletions test/array-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var test = require("tape");
var asObject = require("../object");
var asArray = require("../array");

test("as/array >> as/object", function (tape) {
var deepEqual = Function.prototype.apply.bind(tape.deepEqual, null);


// Basic functionality
// -----------------------------------------------------------------------------------------------

[ [ asObject(asArray({a: "b", c: "d"}))
, {a: "b", c: "d"}
, "shouldn't change a simple object"
]

, [ asObject(asArray({}))
, {}
, "shouldn't change an empty object"
]

, [ asObject(asArray(
{ a: null
, b: 0
, c: true
, d: false
, e: undefined
, f: "string"
, g: ""
, h: ["array"]
}))
, { a: null
, b: 0
, c: true
, d: false
, e: undefined
, f: "string"
, g: ""
, h: ["array"]
}
, "shouldn't change an object with various data types"
]

, [ asObject(asArray
( {a: "b", c: "d", e: {f: "g", h: {i: "j"}}}
))
, {a: "b", c: "d", e: {f: "g", h: {i: "j"}}}
, "shouldn't change a nested object"
]

, [ asObject(asArray
( {a: "b", c: "d", e: {f: "g", h: {i: "j"}}}
, {depth: 1}
), {depth: 1})
, {a: "b", c: "d", e: {f: "g", h: {i: "j"}}}
, "shouldn't change a nested object mapped to a specific depth"
]

, [ asObject(asArray
( {a: "b", c: "d", e: {f: "g", h: {i: "j"}}}
, {depth: Infinity}
), {depth: Infinity})
, {a: "b", c: "d", e: {f: "g", h: {i: "j"}}}
, "shouldn't change a nested object mapped deeply"
]

].map(deepEqual);

tape.end();
});
84 changes: 73 additions & 11 deletions test/array.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,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"
]

, [ 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"
]


// `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"
]

, [ mapToArray({a: 'b', c: 'd'})
, [ {key: 'a', value: 'b'}
, {key: 'c', value: 'd'}
, [ 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"
]

, [ mapToArray({a: 'b', c: 'd', e: {f: 'g'}})
, [ {key: 'a', value: 'b'}
, {key: 'c', value: 'd'}
, {key: 'e', value: {f: 'g'}}
, [ 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);
Expand Down
6 changes: 4 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
require('./array');
require('./object');
require("./array");
require("./object");

require("./array-object");

0 comments on commit e5d1d90

Please sign in to comment.