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

ufer-object

v1.1.1

Published

Support typescript, node-typescript and reactjs. Deep copy object, deep copy array. Deep compare objects, deep copy arrays. Deep freeze object, deep freeze array.

Downloads

7

Readme

Table of contents

API

Deep compare - deepCompare

1. Compare objects

import { deepCompare } from 'ufer-object';

const objA = {
    a: {
        a: 1,
        b: 2,
        c: [1,2,3,4,5,6,NaN,Infinity]
    },
    b: NaN,
    c: [1,2,3,4,5],
    d: () => {
        return 'ok';
    },
    e: null,
    f: '',
    g: 'ok'
}

const objB = {
    b: NaN,
    c: [1,2,3,4,5],
    a: {
        c: [1,2,3,4,5,6,NaN,Infinity],
        a: 1,
        b: 2,
    },
    d: () => {
        return 'ok';
    },
    e:null,
    f: '',
    g: 'ok'
}

const objC = {
    a: {
        c: [1,2,3,4,5,6,NaN,Infinity],
        a: 1,
        b: 2,
    },
}

deepCompare(objA, objB); // return true
deepCompare(objA, objB, {...objA}, {...objB}); // return true

deepCompare(
    objA,
    objB,
    {...objA, newKey: 'value'}, // add a newKey
    {...objB}
); // return false

deepCompare(objA, objC); // return false;
deepCompare(objA.a, objC.a); // return true

2. Compare arrays

import {deepCompare} from 'ufer-object';

const arr1 = [1, 2, 'a', {a:1, b:2}, NaN];
const arr2 = [1, 2, 'a', {a:1, b:2}, NaN];
const arr3 = [1, 2, 3, 4, 5];

deepCompare(arr1, arr2); // return true;
deepCompare(arr1, arr2, [...arr1]); // return true;
deepCompare(arr1, arr3); // return false;

3. Compare objects and arrays

const objectLikeArray = {
    0: 1,
    1: 2,
    2: 'a',
    3: {a:1, b:2},
    4: NaN,
    length: 5
};

const arr1 = [1, 2, 'a', {a:1, b:2}, NaN];
const arr2 = [1, 2, 'a', {a:1, b:2}, NaN];

deepCompare(objectLikeArray, arr1); // return false;
deepCompare(arr1, arr2); // return true;

4. Compare everything you want

import {deepCompare} from 'ufer-object';

const d1:Date = new Date('2023-01-01T20:31:51.124Z');
const d2:Date = new Date('2023-01-01T20:31:51.124Z');
const toDay:Date = new Date();
const arr = [1, 2, 'a', {a:1, b:2}, NaN];
const obj = {a:1, b:2}

deepCompare(d1, d2); // return true;
deepCompare(d1, d2, new Date('2023-01-01T20:31:51.124Z')); // return true;
deepCompare(d1, toDay); // return false;
deepCompare(d1, arr); // return false;

// compare NaN
(NaN === NaN) // return false
deepCompare(NaN, NaN); // return true  

// 
deepCompare(Infinity, Infinity); // return true;
deepCompare(Infinity, toDay); // return false;
import {deepCompare} from 'ufer-object';

class Cat {
    private _name:string;
    public constructor(name:string){
        this._name = name;
    }

    public printName(){
        console.log(this._name);
    }
}

class Duck {
    private _name:string;
    public constructor(name:string){
        this._name = name;
    }

    public printName(){
        console.log(this._name);
    }
}

class Dog {
    private _name:string;

    public constructor(name:string){
        this._name = name;
    }

    public get name(){
        return this._name;
    }

    public set name(v:string){
        this._name = v;
    }

    public printName(){
        console.log(this._name);
    }
}

deepCompare(
    new Cat('Daisy'),
    new Cat('Daisy')
); // return true;



deepCompare(
    new Cat('Daisy'),
    new Dog('Daisy')
); // return false;



const rosieDog = new Dog('Rosie');
const sunnyDog = new Dog('Sunny');


// before change the name of the sunnyDog.
deepCompare(rosieDog, sunnyDog); // return false;

sunnyDog.name = 'Rosie'; 

// after change the name of the sunnyDog.
deepCompare(rosieDog, sunnyDog); // return true; 
// because after renaming all properties in rosieDog are same as all properties in sunnyDog and all methods in rosieDog are same as all method sunnyDog.

Deep copy - deepCopy

1. deep-copy everything you want

// deep-copy date ==========================
const someday:Date = new Date('2023-01-01T20:31:51.124Z');
deepCopy(someday);


// deep-copy an object ==========================
const originalObject = {
    a: {
        c: [1,2,3,4,5,6,NaN,Infinity],
        a: 1,
        b: 2,
    },
}
const copiedObject = deepCopy(originalObject);

console.log(copiedObject.a === originalObject.a) // false

// copiedObject.a.b === 2
console.log(copiedObject.a.b === originalObject.a.b) // true



// copy a array ==========================
deepCopy([1, 2, 3, 4, 5, NaN, '7', {a: 1, b: 2}, ]);


// WARNING: Not support deep-copy instance of object which created by custom-class. Consider the following example:
        // const rosieDog = new Dog('Rosie');
        // deepCopy(rosieDog) // ! NOT-SUPPORT

Deep freeze - deepFreeze

import { deepFreeze } from 'ufer-object';

const subject = {
    a: 1,
    b: '2',
    c: [
        null,
        '1',
        2,
        {
            a: 1,
            b: 2
        },
        new Date()
    ]
}

const frozenObject = deepFreeze(subject);

console.log( subject === frozenObject ); // return true

subject.a = 2 // TypeError: Cannot assign to read only property 'a' of object '#<Object>'

subject.c[3] = 2 // TypeError: Cannot assign to read only property '3' of object '[object Array]'

(subject.c[3] as any).a = 2 // Cannot assign to read only property 'a' of object '#<Object>'