Last active
March 20, 2018 16:00
-
-
Save PocketNinjaDesign/fc3a3b7071900598ea48ae80d694d738 to your computer and use it in GitHub Desktop.
ES6 extend method replicating $.extend Passes ESlint checking too! Added immutable arrays line 32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Code taken from http://youmightnotneedjquery.com/ | |
// THEN, edited to es6 and altered to work with ESlint | |
// | |
// 1. extend objects the same as jQuery | |
// 2. passes eslint AirBnB | |
// 3. provides you with isTypeOf and isObject methods | |
// 4. Please remember to use Object.assign instead if object 1 deep | |
// You lucky people. | |
// | |
export default { | |
isTypeOf(str, obj) { | |
return `[object ${str}]` === Object.prototype.toString.call(obj); | |
}, | |
isObject(obj) { | |
return this.isTypeOf('Object', obj); | |
}, | |
extend(..._args) { | |
const out = {}; | |
const args = _args; | |
for (let i = 0; i < args.length; i += 1) { | |
const obj = args[i]; | |
if (obj && this.isObject(obj)) { | |
Object.keys(obj).forEach((key) => { | |
if (this.isObject(obj[key])) { | |
out[key] = this.extend(out[key], obj[key]); | |
} else if (this.isArray(obj[key])) { | |
out[key] = [...obj[key]]; | |
} else { | |
out[key] = obj[key]; | |
} | |
}); | |
} | |
} | |
return out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment