npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pure-object

v2.0.1

Published

Create objects with no inhereted prototype

Downloads

22

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 and getOwnPropertySymbols)
  • 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 and getOwnPropertySymbols)

Benefits

  1. Tinier footprint (memory allocation is smaller than the standard object's)
  2. More logical looping (no need to check hasOwnProperty in for-in loops)
  3. 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 development dist file with NODE_ENV=development
  • build:minified => run webpack to build production dist file with NODE_ENV=production
  • dev => run webpack dev server to run example app / playground
  • dist => runs build and build:minified
  • lint => run ESLint against all files in the src folder
  • prepublish => runs prepublish:compile when publishing
  • prepublish:compile => run lint, test:coverage, transpile:es, transpile:lib, dist
  • test => run AVA test functions with NODE_ENV=test
  • test:coverage => run test but with nyc for coverage checker
  • test:watch => run test, but with persistent watcher
  • transpile:lib => run babel against all files in src to create files in lib
  • transpile:es => run babel against all files in src to create files in es, preserving ES2015 modules (for pkg.module)