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

@chopinlang/helpers

v0.2.3

Published

A collection of useful helper functions

Downloads

5

Readme

Chopin Helpers

A collection of useful helper functions used internally in the Chopin compiler.

Installation

npm install @chopinlang/helpers

Usage

Include the object in your project:

const helpers = require("helpers");

// You can also destructure your assignment to only get the functions you need
const { isArray, isNumber, handleNegativeIndex } = require("helpers");

Functions

Array

arrayFrom

(object: any, [mapFn: (item: any, index: number, array: any[]) -> void, thisArg: any]) -> any[]

Returns an array from any object that can be converted to an array (e.g. an array-like object). Optional mapFn transforms every element of the array, and optional thisArg sets the context for mapFn.

first

(array: any[]) -> any

Returns the first item of an array.

isArray

(object: any) -> boolean

Determine if an object is an array.

isArrayLike

(object: any) -> boolean

Determine if an object is array-like, e.g. has a length property and items stored in numeric indexes.

last

(array: any[]) -> any

Returns the last item of an array.

toArray

(iterable: object[Symbol.iterator]) -> any[]

Converts an iterable object (array-like object) to an array.

Boolean

isBoolean

(value: any) -> boolean

Determine if a value is a boolean value.

Function

curry

(fn) -> fn

Auto-curry a function.

curryN

(length: number, fn) -> fn

Curry a function with specified arity of length.

curriedApply

(args: any[], fn) -> fn

Transform a function that takes multiple arguments into one that takes an array.

pipe

(val: any, fns: function[]) -> any

Pipe a value through a series of functions

pipeFns

(fns: ...function[]) -> function

Compose a series of functions from left-to-right.

withFunctionName

(name: string, fn) -> function

Add a name property to a function.

Null and Undefined

isNil

(value: any) -> boolean

Determine if a value is null or undefined.

isNull

(value: any) -> boolean

Determine if a value is specifically null.

isUndefined

(value: any) -> boolean

Determine if a value is specifically undefined.

nilDefault

(value: any, default: any) -> any

If a value is null or undefined, specify a default value to return. Otherwise, return the value itself.

Numeric

handleNegativeIndex

(index: number, seq: string|array|object) -> number

If second parameter is object, must be an array-like object or other iterable with numeric indexes.

Converts a negative index into its corresponding positive index relative to a sequence's length. Returns zero or positive index as-is.

isBigInt

(value: any) -> boolean

Determine if a value is a BigInteger.

isNumber

(value: any) -> number

Determine if a value is a number.

valueOf

(value: any) -> number

Convert a value into its numeric equivalent.

CAUTION: will return NaN if the value has no direct numeric equivalent. Will return Infinity or -Infinity if the value is too large or small to be represented by a JavaScript number.

Object

assign

(objects: object[]) -> object

Shallowly copies properties of each successive object to the first object in the order they are passed into the function. Mutates the first object.

callInternalToString

`(object: any) -> string

Calls Object.prototype.toString with object as the context.

classToString

(object: any) -> string

Gets the class or constructor name. Works with anonymous function expressions bound to variables, but the following returns an empty string:

let o = new (function () {})();

... so probably DON'T DO THAT.

clone

(object: any) -> any

Make a clone of an object. If the object has a clone method, it uses that. Otherwise, it recursively copies an object's own properties. Note that __proto__ is not an own property.

deepStrictEqual

(a: any, b: any) -> boolean

Recursively checks all properties of both objects for strict equality (===).

extend

(a: object, b: object) -> object

Copies b's properties shallowly onto a. Mutates a.

extendWithAll

(to: object, froms: object[]) -> object

Copies each property on every object in froms onto to. Goes from left-to-right, e.g. in case of conflicts a later argument's property will overwrite one from an earlier object. Mutates to.

forAllProperties

(object: any, fn: (property: any[, key: string, object: any]) -> void|boolean) -> void

Run fn callback on all own properties of any object. Breaks out of the loop if the callback returns false.

forEachProperty

(object: any, fn: (property: any[, key: string, object: any]) -> void|boolean) -> void

Run fn callback on the enumerable own properties of any object. Breaks out of the loop if the callback returns false.

getClass

(object: any) -> function

Get the constructor function or class used to create an object.

getOwn

(object: any, property: string) -> any

If an object has an own property, retrieve it. Otherwise returns false.

getProto

(object: any) -> any

Get the prototype of an object.

hasOwn

(object: any, property: string) -> boolean

Determine if an object has an own property.

hasProperty

(object: any, property: string) -> boolean

Determine if an object has a property anywhere in its prototype chain.

isObject

(value: any) -> boolean

Determine if a value is an instance of Object. Also detects objects with null prototypes.

isObjectType

(value: any) -> boolean

Determine if value typeof "object" is true.

mapObject

(object: object, fn: (any) -> any) -> object

Returns a new object with the same keys as the source object and a callback function applied to each of the source object's properties.

objectIs

(a: any, b: any) -> boolean

Checks if a and b are the exact same object. If primitive values and not NaN, checks for strict equality. Also correctly checks if NaN equals NaN.

ownPropertyNames

(object: any) -> string[]

Return an array of all an object's own string property names.

setAccessor

(target: object, key: string[, { getter: () -> any, setter: (...args: any) -> any }]) -> void

Sets a getter and setter (either or both optional) on target[key].

setProperty

(target: object, name: string, value: any[, enumerable: boolean|undefined, writable: boolean|undefined]) -> void

Sets a property and its attributes on a target object.

RegExp

isRegExp

(value: any) -> boolean

Determine if a value is a RegExp.

String

coerceToString

(value: any) -> string

If a value is not a string, coerce it to its string representation.

isString

(value: any) -> boolean

Determine if a value is a string.

trim

(string: string) -> string

Trims whitespace from both sides of a string.

Symbol

isSymbol

(value: any) -> boolean

Determine if a value is a Symbol.

Other Values

isDefined

(value: any) -> boolean

Determine if a value is defined.

Constants

__

Use as a placeholder for curry and curryN.