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

zana-util

v0.2.2

Published

The utility class for the Zana package

Downloads

15

Readme

zana-util

A utility library for deep equals, cloning, extending, and type checking.

import { clone, equals, type, types } from 'zana-util';

let arr = [1, 2, 3, 4, 5];
let arr2 = clone(arr);
console.log(arr === arr2); //=> false
console.log(equals(arr, arr2)); //=> true
console.log(type(arr) === types.array) //=> true

Installation

npm install zana-util

Default Task

  • Install node.js
  • Clone the zana-util project
  • Run npm install
  • Run gulp
    • Executes tests
    • Cleans dist
    • Lints source
    • Builds source
    • Watches source and tests

Usage

clone

clone(val) will make deep copies of values, including primitives, arrays, objects, buffers, dates, regular expressions, sets, maps, and classes.

import { clone } from 'zana-util';

let obj1 = { a: 1, b: 2, c: 3 };
let obj2 = clone(obj1);
console.log(obj1 === obj2); //=> false
obj1.a = 4;
console.log(obj1); //=> { a: 4, b: 2, c: 3 }
console.log(obj2); //=> { a: 1, b: 2, c: 3 }

Note that all nested values will also be cloned.

let arr1 = [
    { a: 1, b: 2, c: [3, 4, 5] },
    { a: 1, b: 2, c: [3, 4, 5] }
];
let arr2 = clone(arr1);
console.log(arr1 === arr2); //=> false
console.log(arr1[0].c === arr2[0].c); //=> false
arr1[0].c.push(6);
console.log(arr1[0]); //=> { a: 1, b: 2, c: [3, 4, 5, 6] }
console.log(arr2[0]); //=> { a: 1, b: 2, c: [3, 4, 5] }

equals

equals(a, b) will make a deep comparison of two values.

import { equals } from 'zana-util';

let obj1 = { a: 1, b: 2, c: 3 };
let obj2 = { a: 1, b: 2, c: 3 };
console.log(obj1 === obj2); //=> false
console.log(equals(obj1, obj2)); //=> true

As with clone, all nested values will be compared.

let arr1 = [
    { a: 1, b: 2, c: [3, 4, 5] },
    { a: 1, b: 2, c: [3, 4, 5] }
];
let arr2 = [
    { a: 1, b: 2, c: [3, 4, 5] },
    { a: 1, b: 2, c: [3, 4, 5] }
];
console.log(arr1 === arr2); //=> false
console.log(equals(arr1, arr2)); //=> true

extend

extend(obj, ...objects) will extend the first argument with any number of following arguments.

import { extend } from 'zana-util';

let obj1 = { a: 1, b: 2 };
let obj2 = { c: 3 };
extend(obj1, obj2);
console.log(obj1); //=> { a: 1, b: 2, c: 3 }

If the first item contains a property, it will not be overwritten by any of the extensions.

let obj1 = { a: 1, b: 2 };
let obj2 = { a: 9, c: 3 };
extend(obj1, obj2);
console.log(obj1); //=> { a: 1, b: 2, c: 3 }

As well as Objects, extend will also work with other iterable items such as Arrays, Sets, and Maps.

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6, 7];
extend(arr1, arr2);
console.log(...arr1); //=> [ 1, 2, 3, 7 ]

let set1 = new Set([1, 2, 3]);
let set2 = new Set([4]);
extend(set1, set2);
console.log(...set1); //=> 1 2 3 4

let map1 = new Map([['a', 1], ['b', 2]]);
let map2 = new Map([['c', 3]]);
extend(map1, map2);
console.log(...map1); //=> [ 'a', 1 ] [ 'b', 2 ] [ 'c', 3 ]

extend will also work with nested values, but will extend references to non primitive values wherever the property is not defined on the first value.

let obj1 = { a: 1, c: [2] };
let obj2 = { b: [3, 4, 5], c: [6, 7] };
extend(obj1, obj2);
console.log(obj1); //=> { a: 1, b: [3, 4, 5], c: [2, 7] }
console.log(obj1.b === obj2.b); //=> true
console.log(obj1.c === obj2.c); //=> false

each

each(iterable) will yield a key value pair for iterable items.

import { each } from 'zana-util';

let arr = ['a', 'b', 'c'];
for (let [key, val] of each(arr))
    console.log(key, val);

// console output
//=> 0, 'a'
//=> 1, 'b'
//=> 2, 'c'

let obj = { a: 1, b: 2, c: 3 };
for (let [key, val] of each(obj))
    console.log(key, val);

// console output
//=> 'a', 1
//=> 'b', 2
//=> 'c', 3

let map = new Map([['a', 1], ['b', 2], ['c', 3]]);
for (let [key, val] of each(map))
    console.log(key, val);

// console output
//=> 'a', 1
//=> 'b', 2
//=> 'c', 3

let set = new Set([1, 2, 3]);
for (let [key, val] of each(set))
    console.log(key, val);

// console output
//=> 1, 1
//=> 2, 2
//=> 3, 3

type

type(val) will return the type of a value.

import { type, types } from 'zana-util';

console.log(type(arguments)     === types.arguments); //=> true
console.log(type([])            === types.array);     //=> true
console.log(type(true)          === types.boolean);   //=> true
console.log(type(new Date())    === types.date);      //=> true
console.log(type(new Error())   === types.error);     //=> true
console.log(type(function(){})  === types.function);  //=> true
console.log(type(new Map())     === types.map);       //=> true
console.log(type(null)          === types.null);      //=> true
console.log(type(0)             === types.number);    //=> true
console.log(type({})            === types.object);    //=> true
console.log(type(new Promise()) === types.promise);   //=> true
console.log(type(/.*/)          === types.regexp);    //=> true
console.log(type('')            === types.string);    //=> true
console.log(type(new Set())     === types.set);       //=> true
console.log(type(Symbol())      === types.symbol);    //=> true
console.log(type(undefined)     === types.undefined); //=> true
console.log(type(new WeakMap()) === types.weakmap);   //=> true
console.log(type(new WeakSet()) === types.weakset);   //=> true