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

cute-js

v1.1.18

Published

Common Utilities

Downloads

4

Readme

  • This Library contains some common helpful tools that I use the most as web developer.
  • For documentation, A type annotation that is similar to Haskell is chosen as it's very compact and clear to read.
  1. Examples

  2. Guide for Type annotation

  3. List of Functions

Examples in React:

1. Updating and validating state in React

import React from 'react';
import {recursivelyGetProperties, recursiveAssignObject, deepCloneObject } from 'cute-js';

class SimpleForm extends React.Component {
    state = {
      location: {
          address: null,
          country: null,
          geoLocation: {
              lat: null,
              lng: null
          }
      },
      errorLocation : {
        address: null,
        country: null,
        geoLocation: {
            lat: null,
            lng: null
        }
      }
    }

    render(){
        const errorStyle={ color: 'red' };
        return (
            <form>
                <label>Latitude</label>
                <input
                    value={this.state.location.geoLocation.lat}
                    onChange={this.onFieldChange.bind(this, 'geoLocation.lat')}
                    onBlur={this.onValidating.bind(this, 'geoLocation.lat')}
                />
                <div style={errorStyle}>{this.state.errorLocation.geoLocation.lat}</div>

                <label>Longitude</label>
                <input
                    value={this.state.location.geoLocation.lng}
                    onChange={this.onFieldChange.bind(this, 'geoLocation.lng')}
                    onBlur={this.onValidating.bind(this, 'geoLocation.lng')}
                />
                <div style={errorStyle}>{this.state.errorLocation.geoLocation.lng}</div>

                <label>Address</label>
                <input
                    value={this.state.location.address}
                    onChange={this.onFieldChange.bind(this, 'address')}
                    onBlur={this.onValidating.bind(this, 'address')}
                />
                <div style={errorStyle}>{this.state.errorLocation.address}</div>


                <label>Country</label>
                <input
                    value={this.state.location.country}
                    onChange={this.onFieldChange.bind(this, 'country')}
                    onBlur={this.onValidating.bind(this, 'country')}
                />
                <div style={errorStyle}>{this.state.errorLocation.country}</div>
            </form>
        );
    }

    // onFieldChange = fieldPath:String -> e:Object{Event} -> null
    onFieldChange = (fieldPath, e) => {
        const value = e.target.value;
        const cloneState = deepCloneObject(this.state);
        const { location } = cloneState;
        recursiveAssignObject(location, fieldPath, value );
        this.setState({ location });
    }

    // onValidating = fieldPath:String -> e:Object{Event} -> null
    onValidating = (fieldPath, e) => {
        const value = recursivelyGetProperties(this.state.location, fieldPath );
        const cloneError = deepCloneObject(this.state.errorLocation);
        const isValid = !!value;
        console.log(isValid);
        if(!isValid){
            recursiveAssignObject(cloneError, fieldPath, 'NOT VALID')
        } else {
            recursiveAssignObject(cloneError, fieldPath, '')     
        }
        this.setState({ errorLocation: cloneError });
    }
}

export default SimpleForm;

Guide for Type annotation

For documentation, A type annotation that is similar to Haskell is chosen as it's very compact and clear to read.

Example Of Types:

type1

recursiveAssignObject = obj:Obj{Any} -> path:String -> value:Any -> null
  1. the name of function is recursiveAssignObject
  2. This function return null
  3. It accepts, 3 parameters: (obj, path, value).

type2

onFieldChange = fieldPath:String -> e:Object{Event} -> Object { Any }
  1. the name of function is onFieldChange
  2. This function return Object of Any structure
  3. It accepts, 2 parameters: (fieldPath, e).

List of Functions:

1.Deep Clone Object

  • This function will clone an object, a very simple and fast immutability function.
  • It works with array too, although it's more efficient to use Array.slice() instead.
  • Cloning will preserve the instance of object, that's why it will preserve Array as Array not an Object (Most clone library will not preserve it).
TypeAnnotation:
deepCloneObject = obj:Obj{Any} -> Options:Obj{ excludeKey:Bool } -> Obj{ Any }

example:

import { deepCloneObject } from 'cute-js';        
const obj = {
    name: 'Joe',
    age: 23,
    address: {
        street: 'somewhere drv, no.23',
        postalCode: '11520',
        geoLocation: {
            lat: -6.12314,
            lon: 108.123123
        }
    },
}

const newObj = deepCloneObject(obj);

newObj.age = 5;
newObj.postalCode = '1';

To cater a need to clone an instance of class. deepCloneObject will still preserve the instance of class.

example:

import { deepCloneObject } from 'cute-js';        
class Car {
    constructor(name, made){
        this.name = name
        this.made = made
    }
}

const aCar = new Car('Mazda',2018);
const newCar = deepCloneObject(aCar);

console.log(newCar instanceOf Car); // true

2. Recursive Assign Value to Object

  • This function will assign value according to path provided to an object.
  • When accessing properties on object that has deep nested properties, Vanilla js will just throw an error that is not helpful. With this function it will show the object and the path that fails.
  • Warning: this function mutate the object, so use it with deepCloneObject to achieve immutability. example:
TypeAnnotation:
recursiveAssignObject = Obj{Any} -> path:String -> value:Any -> null
import { recursiveAssignObject } from 'cute-js';
    
const obj = {
    name: 'Joe',
    age: 23,
    address: {
        street: 'somewhere drv, no.23',
        postalCode: '11520',
        geoLocation: {
            lat: -6.12314,
            lon: 108.123123
        }
    },
}

recursiveAssignObject(obj, 'address.postalCode', 123);
recursiveAssignObject(obj, 'address.geoLocation.lat', 0);
obj.address.postalCode === 123; // true
obj.address.geoLocation.lat === 0; // true

3. Recursive Get Value from Object

  • This function will retrieve value according to path provided to an object.
  • When accessing properties on object that has deep nested properties, Vanilla js will just throw an error that is not helpful. With this function it will show the object and the path that fails.
TypeAnnotation:
recursivelyGetProperties = Obj {} -> path:String -> value:Any
import { recursivelyGetProperties } from 'cute-js';
    
const obj = {
    name: 'Joe',
    age: 23,
    address: {
        street: 'somewhere drv, no.23',
        postalCode: '11520',
        geoLocation: {
            lat: -6.12314,
            lon: 108.123123
        }
    },
}

recursivelyGetProperties(obj, 'address.postalCode'); // '11520'
recursivelyGetProperties(obj, 'address.geoLocation.lat'); //-6.12314