pure-object
v2.0.1
Published
Create objects with no inhereted prototype
Downloads
12
Readme
pure-object
Create objects with no inhereted prototype
Table of Contents
Installation
$ npm i pure-object --save
Usage
// 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
getOwnPropertyNames
andgetOwnPropertySymbols
)
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
getOwnPropertyNames
andgetOwnPropertySymbols
)
- 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
hasOwnProperty
in 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 developmentdist
file with NODE_ENV=developmentbuild:minified
=> run webpack to build productiondist
file with NODE_ENV=productiondev
=> run webpack dev server to run example app / playgrounddist
=> runsbuild
andbuild:minified
lint
=> run ESLint against all files in thesrc
folderprepublish
=> runsprepublish:compile
when publishingprepublish:compile
=> runlint
,test:coverage
,transpile:es
,transpile:lib
,dist
test
=> run AVA test functions withNODE_ENV=test
test:coverage
=> runtest
but withnyc
for coverage checkertest:watch
=> runtest
, but with persistent watchertranspile:lib
=> run babel against all files insrc
to create files inlib
transpile:es
=> run babel against all files insrc
to create files ines
, preserving ES2015 modules (forpkg.module
)