as
Advanced tools
Comparing version 0.2.2 to 0.3.1
61
array.js
@@ -1,25 +0,10 @@ | ||
(function (global, factory) { | ||
var root, exportsName, factoryArguments; | ||
(function (factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
} else { | ||
if (module && typeof module.exports !== "undefined") { | ||
factoryArguments = [module.exports]; | ||
root = module; | ||
exportsName = "exports"; | ||
} else { | ||
factoryArguments = [global.array = {}]; | ||
root = global; | ||
exportsName = "array"; | ||
} | ||
factory.apply(null, factoryArguments); | ||
if (Object.keys(root[exportsName]).length == 1 && root[exportsName].propertyIsEnumerable("default")) { | ||
root[exportsName] = root[exportsName]["default"]; | ||
} | ||
define(["exports", "module"], factory); | ||
} else if (typeof exports !== "undefined" && typeof module !== "undefined") { | ||
factory(exports, module); | ||
} | ||
})(this, function (exports) { | ||
"use strict" | ||
})(function (exports, module) { | ||
"use strict"; | ||
exports["default"] = asArray; | ||
// | ||
@@ -37,3 +22,3 @@ // as/array | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* - {Number} [depth=1] | ||
* The depth to which the `object`'s tree should be mapped. Set it to `Infinity` to map the | ||
@@ -45,21 +30,27 @@ * entire tree structure. | ||
*/ | ||
function asArray(object, options, _depthLeft) { | ||
var key, value; | ||
module.exports = asArray; | ||
function asArray(object, options) { | ||
// Parse options. | ||
var depth = !options || typeof options == "undefined" ? 1 : options.depth; | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
// End recursion if we've reached a depth of 0. | ||
if (!depth) return object; | ||
if (object instanceof Array) return object.slice(); | ||
// Create an empty `result` array. | ||
var result = []; | ||
for (key in object) if (object.hasOwnProperty(key)) { | ||
value = object[key]; | ||
if (_depthLeft && value !== null && typeof value == "object") { | ||
value = asArray(value, options, _depthLeft - 1); | ||
// For every `key` of the given `object`: | ||
for (var key in object) { | ||
if (object.hasOwnProperty(key)) { | ||
// Can't use iterator because of https://6to5.org/docs/usage/caveats/ | ||
var value = object[key]; | ||
// - recurse if the value is an object | ||
if (typeof value == "object" && value !== null) { | ||
value = asArray(value, { depth: depth - 1 }); | ||
} | ||
// - append {key: `key`, value: `object[key]`} to `result` | ||
result.push({ key: key, value: value }); | ||
} | ||
result.push({ key: key, value: value }); | ||
} | ||
} // Return the `result`. | ||
return result; | ||
} | ||
}); |
@@ -13,3 +13,3 @@ // | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* - {Number} [depth=1] | ||
* The depth to which the `object`'s tree should be mapped. Set it to `Infinity` to map the | ||
@@ -21,22 +21,28 @@ * entire tree structure. | ||
*/ | ||
export default function asArray (object, options, _depthLeft) { | ||
var key, value; | ||
export default function asArray (object, options) { | ||
// Parse options. | ||
var depth = | ||
( !options || typeof options == "undefined" | ||
? 1 | ||
: options.depth | ||
); | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
// End recursion if we've reached a depth of 0. | ||
if (!depth) return object; | ||
if (object instanceof Array) return object.slice(); | ||
// Create an empty `result` array. | ||
var result = []; | ||
for (key in object) if (object.hasOwnProperty(key)) { | ||
value = object[key]; | ||
if ( _depthLeft | ||
&& value !== null && typeof value == "object" | ||
) { | ||
value = asArray(value, options, _depthLeft - 1); | ||
// For every `key` of the given `object`: | ||
for (let key in object) if (object.hasOwnProperty(key)) { // Can't use iterator because of https://6to5.org/docs/usage/caveats/ | ||
let value = object[key]; | ||
// - recurse if the value is an object | ||
if (typeof value == "object" && value !== null) { | ||
value = asArray(value, {depth: depth - 1}); | ||
} | ||
// - append {key: `key`, value: `object[key]`} to `result` | ||
result.push({key: key, value: value}); | ||
} | ||
// Return the `result`. | ||
return result; | ||
} |
@@ -13,3 +13,3 @@ // | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* - {Number} [depth=1] | ||
* The depth to which the `array`'s pairs should be traversed. Set it to `Infinity` to map the | ||
@@ -21,21 +21,35 @@ * whole structure. | ||
*/ | ||
export default function asObject (array, options, _depthLeft) { | ||
var pair, value; | ||
export default function asObject (array, options) { | ||
// Parse options. | ||
var depth = | ||
( !options || typeof options == "undefined" | ||
? 1 | ||
: options.depth | ||
); | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
// End recursion if we've reached a depth of 0. | ||
if (!depth) return array; | ||
// Create an empty `result` object. | ||
var result = {}; | ||
var i = 0; var l = array.length; while (i < l) { | ||
pair = array[i++]; | ||
// For every `pair` of the given `array`: | ||
var i = 0; var l = array.length; | ||
while (i < l) { let pair = array[i++]; | ||
// - skip the `pair` if it has no `key` | ||
if (!pair || !pair.hasOwnProperty("key")) continue; | ||
value = pair.value; | ||
if (_depthLeft && value instanceof Array) { | ||
value = asObject(value, options, _depthLeft - 1); | ||
// - save `pair.value` as `value` | ||
let value = pair.value; | ||
// - recurse if the `value` is an array | ||
if (value instanceof Array) { | ||
value = asObject(value, {depth: depth - 1}); | ||
} | ||
// - save `value` as `result[pair.key]` | ||
result[pair.key] = value; | ||
} | ||
// Return the `result`. | ||
return result; | ||
} |
@@ -26,3 +26,3 @@ /*jshint node: true, globalstrict: true */ | ||
} | ||
} | ||
}; | ||
@@ -60,3 +60,3 @@ | ||
return gulp.src(settings.scripts.source) | ||
.pipe(to5({modules: "6-to-library"})) | ||
.pipe(to5({modules: "umd"})) | ||
.pipe(gulp.dest(settings.scripts.target.es5)) | ||
@@ -63,0 +63,0 @@ ; |
32
index.js
@@ -1,27 +0,11 @@ | ||
(function (global, factory) { | ||
var root, exportsName, factoryArguments; | ||
(function (factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "./array", "./object"], factory); | ||
} else { | ||
if (module && typeof module.exports !== "undefined") { | ||
factoryArguments = [module.exports, require("./array"), require("./object")]; | ||
root = module; | ||
exportsName = "exports"; | ||
} else { | ||
factoryArguments = [global.index = {}, global.array, global.object]; | ||
root = global; | ||
exportsName = "index"; | ||
} | ||
factory.apply(null, factoryArguments); | ||
if (Object.keys(root[exportsName]).length == 1 && root[exportsName].propertyIsEnumerable("default")) { | ||
root[exportsName] = root[exportsName]["default"]; | ||
} | ||
define(["exports", "module", "./array", "./object"], factory); | ||
} else if (typeof exports !== "undefined" && typeof module !== "undefined") { | ||
factory(exports, module, require("./array"), require("./object")); | ||
} | ||
})(this, function (exports, _array, _object) { | ||
"use strict" | ||
})(function (exports, module, _array, _object) { | ||
"use strict"; | ||
var _interopRequire = function (obj) { | ||
return obj && (obj["default"] || obj); | ||
}; | ||
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; }; | ||
@@ -42,3 +26,3 @@ // | ||
exports["default"] = { array: array, object: object }; | ||
module.exports = { array: array, object: object }; | ||
}); |
@@ -1,25 +0,10 @@ | ||
(function (global, factory) { | ||
var root, exportsName, factoryArguments; | ||
(function (factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
} else { | ||
if (module && typeof module.exports !== "undefined") { | ||
factoryArguments = [module.exports]; | ||
root = module; | ||
exportsName = "exports"; | ||
} else { | ||
factoryArguments = [global.object = {}]; | ||
root = global; | ||
exportsName = "object"; | ||
} | ||
factory.apply(null, factoryArguments); | ||
if (Object.keys(root[exportsName]).length == 1 && root[exportsName].propertyIsEnumerable("default")) { | ||
root[exportsName] = root[exportsName]["default"]; | ||
} | ||
define(["exports", "module"], factory); | ||
} else if (typeof exports !== "undefined" && typeof module !== "undefined") { | ||
factory(exports, module); | ||
} | ||
})(this, function (exports) { | ||
"use strict" | ||
})(function (exports, module) { | ||
"use strict"; | ||
exports["default"] = asObject; | ||
// | ||
@@ -37,3 +22,3 @@ // as/object | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* - {Number} [depth=1] | ||
* The depth to which the `array`'s pairs should be traversed. Set it to `Infinity` to map the | ||
@@ -45,22 +30,34 @@ * whole structure. | ||
*/ | ||
function asObject(array, options, _depthLeft) { | ||
var pair, value; | ||
module.exports = asObject; | ||
function asObject(array, options) { | ||
// Parse options. | ||
var depth = !options || typeof options == "undefined" ? 1 : options.depth; | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
// End recursion if we've reached a depth of 0. | ||
if (!depth) return array; | ||
// Create an empty `result` object. | ||
var result = {}; | ||
var i = 0;var l = array.length;while (i < l) { | ||
pair = array[i++]; | ||
// For every `pair` of the given `array`: | ||
var i = 0;var l = array.length; | ||
while (i < l) { | ||
var pair = array[i++]; | ||
// - skip the `pair` if it has no `key` | ||
if (!pair || !pair.hasOwnProperty("key")) continue; | ||
value = pair.value; | ||
if (_depthLeft && value instanceof Array) { | ||
value = asObject(value, options, _depthLeft - 1); | ||
// - save `pair.value` as `value` | ||
var value = pair.value; | ||
// - recurse if the `value` is an array | ||
if (value instanceof Array) { | ||
value = asObject(value, { depth: depth - 1 }); | ||
} | ||
// - save `value` as `result[pair.key]` | ||
result[pair.key] = value; | ||
} | ||
// Return the `result`. | ||
return result; | ||
} | ||
}); |
{ "name": "as" | ||
, "version": "0.2.2" | ||
, "version": "0.3.1" | ||
, "description": "as/array and as/object. Convert easily, back and forth." | ||
@@ -44,6 +44,5 @@ , "license": "MIT" | ||
, "gulp": "3.8.10" | ||
, "gulp-6to5": "2.0.0" | ||
, "6-to-library": "0.3.0" | ||
, "gulp-6to5": "3.0.0" | ||
, "del": "1.1.1" | ||
} | ||
} |
@@ -13,3 +13,3 @@ // | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* - {Number} [depth=1] | ||
* The depth to which the `object`'s tree should be mapped. Set it to `Infinity` to map the | ||
@@ -21,22 +21,28 @@ * entire tree structure. | ||
*/ | ||
export default function asArray (object, options, _depthLeft) { | ||
var key, value; | ||
export default function asArray (object, options) { | ||
// Parse options. | ||
var depth = | ||
( !options || typeof options == "undefined" | ||
? 1 | ||
: options.depth | ||
); | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
// End recursion if we've reached a depth of 0. | ||
if (!depth) return object; | ||
if (object instanceof Array) return object.slice(); | ||
// Create an empty `result` array. | ||
var result = []; | ||
for (key in object) if (object.hasOwnProperty(key)) { | ||
value = object[key]; | ||
if ( _depthLeft | ||
&& value !== null && typeof value == "object" | ||
) { | ||
value = asArray(value, options, _depthLeft - 1); | ||
// For every `key` of the given `object`: | ||
for (let key in object) if (object.hasOwnProperty(key)) { // Can't use iterator because of https://6to5.org/docs/usage/caveats/ | ||
let value = object[key]; | ||
// - recurse if the value is an object | ||
if (typeof value == "object" && value !== null) { | ||
value = asArray(value, {depth: depth - 1}); | ||
} | ||
// - append {key: `key`, value: `object[key]`} to `result` | ||
result.push({key: key, value: value}); | ||
} | ||
// Return the `result`. | ||
return result; | ||
} |
@@ -13,3 +13,3 @@ // | ||
* @param {Object} [options] | ||
* - {Number} [depth=0] | ||
* - {Number} [depth=1] | ||
* The depth to which the `array`'s pairs should be traversed. Set it to `Infinity` to map the | ||
@@ -21,21 +21,35 @@ * whole structure. | ||
*/ | ||
export default function asObject (array, options, _depthLeft) { | ||
var pair, value; | ||
export default function asObject (array, options) { | ||
// Parse options. | ||
var depth = | ||
( !options || typeof options == "undefined" | ||
? 1 | ||
: options.depth | ||
); | ||
if (!options) options = {}; | ||
if (_depthLeft === void null && options.depth) _depthLeft = options.depth; | ||
// End recursion if we've reached a depth of 0. | ||
if (!depth) return array; | ||
// Create an empty `result` object. | ||
var result = {}; | ||
var i = 0; var l = array.length; while (i < l) { | ||
pair = array[i++]; | ||
// For every `pair` of the given `array`: | ||
var i = 0; var l = array.length; | ||
while (i < l) { let pair = array[i++]; | ||
// - skip the `pair` if it has no `key` | ||
if (!pair || !pair.hasOwnProperty("key")) continue; | ||
value = pair.value; | ||
if (_depthLeft && value instanceof Array) { | ||
value = asObject(value, options, _depthLeft - 1); | ||
// - save `pair.value` as `value` | ||
let value = pair.value; | ||
// - recurse if the `value` is an array | ||
if (value instanceof Array) { | ||
value = asObject(value, {depth: depth - 1}); | ||
} | ||
// - save `value` as `result[pair.key]` | ||
result[pair.key] = value; | ||
} | ||
// Return the `result`. | ||
return result; | ||
} |
@@ -53,4 +53,4 @@ var test = require("tape"); | ||
( {a: "b", c: "d", e: {f: "g", h: {i: "j"}}} | ||
, {depth: 1} | ||
), {depth: 1}) | ||
, {depth: 2} | ||
), {depth: 2}) | ||
, {a: "b", c: "d", e: {f: "g", h: {i: "j"}}} | ||
@@ -57,0 +57,0 @@ , "shouldn't change a nested object mapped to a specific depth" |
@@ -18,2 +18,14 @@ var test = require("tape"); | ||
, [ asArray( | ||
{ "_-a": "b" | ||
, "1@'ć /=\\\"": "d" | ||
, "ಠ_ಠ": "it works!" | ||
}) | ||
, [ {key: "_-a", value: "b"} | ||
, {key: "1@'ć /=\\\"", value: "d"} | ||
, {key: "ಠ_ಠ", value: "it works!"} | ||
] | ||
, "should work with special characters and unicode" | ||
] | ||
, [ asArray({}) | ||
@@ -33,2 +45,3 @@ , [] | ||
, h: ["array"] | ||
, i: {object: "object"} | ||
}) | ||
@@ -43,2 +56,3 @@ , [ {key: "a", value: null} | ||
, {key: "h", value: ["array"]} | ||
, {key: "i", value: {object: "object"}} | ||
] | ||
@@ -60,3 +74,3 @@ , "should work for various data types" | ||
, [ asArray({a: "b", c: "d", e: {f: "g"}}, {depth: 1}) | ||
, [ asArray({a: "b", c: "d", e: {f: "g"}}, {depth: 2}) | ||
, [ {key: "a", value: "b"} | ||
@@ -67,6 +81,6 @@ , {key: "c", value: "d"} | ||
] | ||
, "should map one level deep" | ||
, "should map two levels deep" | ||
] | ||
, [ asArray({a: "b", c: "d", e: {f: "g", h: {i: "j"}}}, {depth: 1}) | ||
, [ asArray({a: "b", c: "d", e: {f: "g", h: {i: "j"}}}, {depth: 2}) | ||
, [ {key: "a", value: "b"} | ||
@@ -78,3 +92,3 @@ , {key: "c", value: "d"} | ||
] | ||
, "should map only one level deep" | ||
, "should map only two levels deep" | ||
] | ||
@@ -81,0 +95,0 @@ |
@@ -19,2 +19,14 @@ var test = require("tape"); | ||
, [ asObject( | ||
[ {key: "_-a", value: "b"} | ||
, {key: "1@'ć /=\\\"", value: "d"} | ||
, {key: "ಠ_ಠ", value: "it works!"} | ||
]) | ||
, { "_-a": "b" | ||
, "1@'ć /=\\\"": "d" | ||
, "ಠ_ಠ": "it works!" | ||
} | ||
, "should work with special characters and unicode" | ||
] | ||
, [ asObject([]) | ||
@@ -79,6 +91,6 @@ , {} | ||
] | ||
, {depth: 1} | ||
, {depth: 2} | ||
) | ||
, {a: "b", c: {d: "e"}} | ||
, "should map one level deep" | ||
, "should map two levels deep" | ||
] | ||
@@ -92,6 +104,6 @@ | ||
] | ||
, {depth: 1} | ||
, {depth: 2} | ||
) | ||
, {a: "b", c: {d: "e", f: [{key: "g", value: "h"}]}} | ||
, "should map only one level deep" | ||
, "should map only two levels deep" | ||
] | ||
@@ -98,0 +110,0 @@ |
24006
6
639