@fiverr/futile
Advanced tools
Comparing version 1.7.0 to 1.7.1
/** | ||
* @module lib/deepasign | ||
* @module lib/deepassign | ||
* @since 1.7.0 | ||
@@ -12,3 +12,3 @@ */ | ||
* | ||
* @example deepasign({hash: {a: 1}}, {hash: {b: 2, c: 0}}, {hash: {c: 3}}) // {hash: {a: 1, b:2, c: 3}} | ||
* @example deepassign({hash: {a: 1}}, {hash: {b: 2, c: 0}}, {hash: {c: 3}}) // {hash: {a: 1, b:2, c: 3}} | ||
*/ | ||
@@ -45,2 +45,7 @@ module.exports = (target, ...others) => { | ||
function assign(to, from) { | ||
if (Array.isArray(to) && Array.isArray(from)) { | ||
assignArray(to, from); | ||
return to; | ||
} | ||
to = Object(to); | ||
@@ -76,2 +81,11 @@ from = Object(from); | ||
/** | ||
* Assign members of second array to first | ||
* @private | ||
* @param {Array} to Target array to assign to | ||
* @param {Array} from) The array from which to assign respective values | ||
* no return value | ||
*/ | ||
const assignArray = (to, from) => from.forEach((item, index) => assignable(to[index]) && assignable(item) ? assign(to[index], item) : to.push(item)); | ||
/** | ||
* Perform getOwnPropertySymbols when available | ||
@@ -93,5 +107,16 @@ * @private | ||
function assignKey(to, from, key) { | ||
const easyAssign = !to.hasOwnProperty(key) || !isObject(from[key]); | ||
const easyAssign = !to.hasOwnProperty(key) || !assignable(from[key]); | ||
to[key] = easyAssign ? from[key] : assign(Object.assign({}, to[key]), from[key]); | ||
to[key] = easyAssign ? from[key] : assign( | ||
clone(to[key]), | ||
from[key] | ||
); | ||
} | ||
/** | ||
* Create a shallow clone of a given Object or Array | ||
* @private | ||
* @param {Object|Array} instance Object or Array to clone | ||
* @return {Object|Array} (respectively) a shallow clone | ||
*/ | ||
const clone = (instance) => Array.isArray(instance) ? instance.slice(0) : Object.assign({}, instance); |
@@ -74,2 +74,34 @@ const expect = require('chai').expect; | ||
it('merges arrays', () => { | ||
const x = [1]; | ||
const y = [2]; | ||
deepAssign(x, y); | ||
expect(x).to.deep.equal([1, 2]); | ||
}); | ||
it('merges nested arrays', () => { | ||
const x = {a: 1, x: [1]}; | ||
const y = {x: [2]}; | ||
deepAssign(x, y); | ||
expect(x).to.deep.equal({a: 1, x: [1, 2]}); | ||
}); | ||
it('merges arrays with objects', () => { | ||
const x = {a: 1, x: [{a: 1}]}; | ||
const y = {x: [{b: 2}, {c: 3}]}; | ||
deepAssign(x, y); | ||
expect(x).to.deep.equal({a: 1, x: [{a: 1, b: 2}, {c: 3}]}); | ||
}); | ||
it('merges nested object in array', () => { | ||
const x = {x: [{a: 1}]}; | ||
const y = {x: [{b: 2}]}; | ||
deepAssign(x, y); | ||
expect(x).to.deep.equal({x: [{a: 1, b: 2}]}); | ||
}); | ||
it('handles null and undefined', () => { | ||
@@ -76,0 +108,0 @@ const x = {a: 1, x: {a: 1, b: 2}}; |
'use strict'; | ||
const should = require('chai').should(); // eslint-disable-line no-unused-vars | ||
const URI = require('./'); | ||
const benchmark = (method, count = 100000) => { | ||
const BENCHMARK_COUNTER = 100000; | ||
const humanNumber = (num) => num.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,'); | ||
const benchmark = (method, count = BENCHMARK_COUNTER) => { | ||
while (count--) { | ||
@@ -13,10 +11,34 @@ method(); | ||
const URI = require('./'); | ||
const URL = require('url'); | ||
const EXAMPLE_URL = 'https://www.website.com/page/file.ext?param1=1¶m2=2#param3=3¶m4=4'; | ||
const _uri = () => new URI(EXAMPLE_URL); | ||
const _url = () => URL.parse(EXAMPLE_URL); | ||
describe('uri', () => { | ||
it('100,000 rounds each'); | ||
it(`${humanNumber(BENCHMARK_COUNTER)} rounds each`); | ||
describe('properties analysis comparison to native "url"', () => { | ||
[ | ||
'host', | ||
'path', | ||
'pathname', | ||
'top', | ||
'hash', | ||
'query' | ||
].forEach((prop) => { | ||
describe(prop, () => { | ||
it('uri', () => { | ||
benchmark(() => _uri()[prop]); | ||
}); | ||
it('Native url', () => { | ||
benchmark(() => _url()[prop]); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('properties analysis', () => { | ||
@@ -23,0 +45,0 @@ [ |
{ | ||
"name": "@fiverr/futile", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"author": "Fiverr", | ||
@@ -33,2 +33,4 @@ "license": "MIT", | ||
"jsdoc": "^3.4.3", | ||
"lodash": "^4.17.4", | ||
"merge-deep": "^3.0.0", | ||
"mocha": "^3.2.0", | ||
@@ -35,0 +37,0 @@ "nyc": "^10.1.2" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
81962
49
2051
10