2.0.1 • Published 7 years ago
pure-object v2.0.1
pure-object
Create objects with no inhereted prototype
Table of Contents
Installation
$ npm i pure-object --saveUsage
// ES2015
import pure from "pure-object";
// CommonJS
const pure = require("pure-object").default;
// script
var pure = window.pureObject.default;
const objectToPurify = {
foo: "bar"
};
// create an object with no prototype
const pureObject = pure(objectToPurify);
console.log(pureObject); // {foo: 'bar'}
console.log(object.toString); // [Function]
console.log(pureObject.toString); // undefined
console.log(Object.getPrototypeOf(pureObject)); // null
// create an object with only the prototypical methods you pass
const pureObjectWithProto = pure(objectToPurify, {
bar() {
console.log("baz");
}
});
console.log(Object.getPrototypeOf(pureObject)); // {bar: [Function]}
console.log(object.toString); // [Function]
console.log(pureObject.toString); // undefined (bar is the only method on the prototype)Signature
pure(object: Object, prototype: Object)
object- Object whose key / value pairs will be shallow-cloned to the pure object with same property descriptor
- Clones both enumerable and non-enumerable properties (uses
getOwnPropertyNamesandgetOwnPropertySymbols)
prototype- Object whose key / value pairs will be assigned to the pure object's prototype (if not passed, prototype is
null) - Clones both enumerable and non-enumerable properties (uses
getOwnPropertyNamesandgetOwnPropertySymbols)
- Object whose key / value pairs will be assigned to the pure object's prototype (if not passed, prototype is
Benefits
- Tinier footprint (memory allocation is smaller than the standard object's)
- More logical looping (no need to check
hasOwnPropertyin for-in loops) - Simplified prototypical chain (only the methods you explicitly set will be on the prototype)
It is very likely that a vast majority of your objects could be made into pure objects and you would never notice the difference.
Browser support
- Chrome (all versions)
- Firefox (all versions)
- Edge (all versions)
- Opera 15+
- IE 9+
- Safari 6+
- iOS 8+
- Android 4+
Development
Standard stuff, clone the repo and npm install dependencies. The npm scripts available:
build=> run webpack to build developmentdistfile with NODE_ENV=developmentbuild:minified=> run webpack to build productiondistfile with NODE_ENV=productiondev=> run webpack dev server to run example app / playgrounddist=> runsbuildandbuild:minifiedlint=> run ESLint against all files in thesrcfolderprepublish=> runsprepublish:compilewhen publishingprepublish:compile=> runlint,test:coverage,transpile:es,transpile:lib,disttest=> run AVA test functions withNODE_ENV=testtest:coverage=> runtestbut withnycfor coverage checkertest:watch=> runtest, but with persistent watchertranspile:lib=> run babel against all files insrcto create files inlibtranspile:es=> run babel against all files insrcto create files ines, preserving ES2015 modules (forpkg.module)