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

instancify

v1.0.7

Published

Simple CoW (copy on write) strategy for javascript data structures.

Downloads

22

Readme

Instancify

Simple CoW (copy on write) strategy for javascript data structures.

Getting Started

npm install instancify

const instancify = require("instancify");
const instance1 = instancify([1,2,3]);

Guarantees

  • All data accessible from instancify is read only

  • No external references to the instancify internal data so must use instancify methods to access data

  • Every write returns a brand new instance with brand new references

  • Can only set an instance once so instance always follow a linear path

Uses

Basic use

const instance1 = instancify([1,2,3]);
const instance2 = instance1.writeNewInstanceWithTransformation(data => data.map(val => val + 1));
//instance1 and instance2 share no refernces with each other

Works with 3rd party libraries like ramda and lodash

const instance1 = instancify([1,2,3]);
const instance2 = instance1.writeNewInstanceWithTransformation(data => R.pipe(R.map(val => val + 1))(data));
//instance2 => [2,3,4]

Read only values

const instance1 = instancify([1,2,3]);
instance1[0] = 2;
//throws an error

Can only set an instance once

const instance1 = instancify([1,2,3]);
const instance2a = instance1.writeNewInstanceWithTransformation(data => data.map(val => val + 1));
const instance2b = instance1.writeNewInstanceWithTransformation(data => data.map(val => val + 2));
//returns instance1 and also calls user defined callback function or logs a warning    

Can add methods to instancify

const instance1 = instancify([1,2,3], {
    customSetters: {
        someTransformation: .....
    }
});
const instance2 = instance1.someTransformation();

Lazy

const instance1 = instancify([1,2,3]);
const addToEachElement = instance1.writeNewInstanceWithTransformation(data => num => data.map(val => val + num);
const instance2 = addToEachElement(5);

Can be used as an interface for third party persistent data libraries

const Immutable = require('immutable');
const map = Immutable.Map({num:10});
const instance1 = instancify(map, {
    shouldClone: false,
    shouldFreeze: false,
    customSetters: {
        changeNum: (data, val) => data.set('num', val)
    },
    customGetters: {
        getNum: (data) => data.get('num')
    }
});
const num1 = instance1.getNum();//10
const instance2 = instance1.changeNum(20);
const num2 = instance2.getNum();//20

API

instancify(data, options)

Methods
  • writeNewInstance
"Sets new instance"
const instance1 = instancify([1,2,3]).writeNewInstance([4,5,6]);
//[4,5,6]
  • writeNewInstanceWithPath
"Sets a property of the new instance"
const instance1 = instancify([{a:1},{b:2}]).writeNewInstanceWithPath([0, "a"], 2);
//[{a:2},{b:2}]
const instance2 = instance1.writeNewInstanceWithPath("[0].a"], 3);
//[{a:3},{b:2}]
  • writeNewInstanceWithTransformation
"Sets the new instance with a transformation"
const instance1 = instancify([{a:1},{b:2}]).writeNewInstanceWithTransformation(function(data) {
    data[0].a = 3;
    data[1].b = 4;
    return data;
});
//[{a:3},{b:4}]
Options
  • customClone
"Replace the clone implementation instancify is using with another"
const instance1 = instancify(data, {
    customClone: //whatever clone implementation you'd like instancify to use
});
  • shouldClone
"Turn cloning on or off"
const instance1 = instancify(data, {
    shouldClone: true//defaults to true
});
  • shouldFreeze
"Turn freezing on or off"
const instance1 = instancify(data, {
    shouldFreeze: true//defaults to true
});
  • customSetters
"Add you own setter methods onto instancify"
const instance1 = instancify(data, {
    customSetters: {
        myOwnMethod: function(data, prop, val) {
            data[prop] = val;
            return data;
        }
    }
});
  • customGetters
"Add you own getter methods onto instancify"
const instance1 = instancify(data, {
    customGetters: {
        getSomething: function(data, prop) {
            return data[prop];
        }
    }
});
  • errorFunction
"Add you own error function for when setter get called on an instance more than once"
const instance1 = instancify(data, {
    errorFunction: function(instance) {
        alert("tried to set this instance twice", instance)
    }
});
  • lift
"Add you own setter methods onto instancify after initializing instancify"
const instance1 = instancify(data);
const instance2 = instance1.lift({
    customSetters: {
        myOwnMethod: function(data, prop, val) {
            data[prop] = val;
            return data;
        }
    },
    customGetters: {
        getSomething: function(data, prop) {
            return data[prop];
        }
    }
});
  • getLatestInstance
"Get the latest instance"
const instance1 = instancify(data);
const instance2 = instance1.writeNewInstanceWithTransformation(someTransformFunction);
console.log(instance1.getLatestInstance() === instance2)
//true

Tests

To run test in root directory use command npm run test

LICENSE

LICENSE