pure-object
Create objects with no inhereted prototype
Table of Contents
Installation
$ npm i pure-object --save
Usage
// ES2015; // CommonJSconst pure = default; // scriptvar pure = windowpureObjectdefault; const objectToPurify = foo: "bar"; // create an object with no prototypeconst pureObject = ; console; // {foo: 'bar'}console; // [Function]console; // undefinedconsole; // null // create an object with only the prototypical methods you passconst pureObjectWithProto = ; console; // {bar: [Function]}console; // [Function]console; // 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
)