1.0.2 • Published 1 year ago

helper-methods-js v1.0.2

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

Helper methods

A package that provides helper methods for common and not-so-common use cases

Contributions are welcome!

Installation

npm install helper-methods-js

Methods


deepcopy

This method creates a deep copy of the provided object to the given depth. Beyond that depth, the object is shallow copied

ParameterDescriptionDefault Value
objThe object to be copiedN/A
max_depth (optional)Maximum depth to which the function would copy the object. Beyond this depth shallow copies would be created. A value of 0 implies that the entire object should be deep copied.0

Sample usage

import { deepcopy } from 'helper-methods-js'

deepcopy({ name: 'bob' }, 0);

dedupe

This method provides implementation to remove duplicates from an array.

ParameterDescriptionDefault Value
arrayThe array that needs duplicates removedN/A
condition (optional)A function that returns either true or false. This condition is executed for each array value and the values for which it returns true are removed. In case this condition is not provided, elements are removed on equality comparison.N/A

Sample usage

import { dedupe } from 'helper-methods-js'

const cars = [
  { name: "audi", owner: "a" },
  { name: "bmw", owner: "x" },
  { name: "audi", owner: "b" },
  { name: "bmw", owner: "b" },
];

dedupe(cars, (e, i) => e.owner === i.owner)

indexesOf

This method provides implementation to return indexes of all elements that match a specific condition

ParameterDescriptionDefault Value
arrayThe array that needs duplicates removedN/A
conditionIt can either be a value or a functions. In case of a function, the function should return true or false. This condition is executed for each array value and the values for which it returns true are added to the list of indexes returned. In case this condition is not provided, elements are removed on equality comparison.N/A

Sample usage

import { indexesOf } from 'helper-methods-js'

const objs = [
  { age: 21, nationality: "a" },
  { age: 24, nationality: "a" },
  { age: 21, nationality: "b" },
  { age: 32, nationality: "c" },
  { age: 42, nationality: "b" },
  { age: 52, nationality: "c" },
  { age: 21, nationality: "a" },
]

const indexes = indexesOf(objs, (i: any) => i.nationality === "b")

isAsyncFunction

This method returns whether a function is async or not.

ParameterDescriptionDefault Value
fnThe function for which it needs to be determined whether it is async or not.N/A

Sample usage

import { isAsyncFunction } from 'helper-methods-js'

const testFn = async () => {};
isAsyncFunction(testFn) // returns true

const testFnSync = () => {};
isAsyncFunction(testFnSync) // returns false

trimDelimeters

This method takes a string and removes the provided delimeters from the start and end of the string.

ParameterDescriptionDefault Value
strThe string to be trimedN/A
delimetersA list of delimeters to be trimned from the start and/or end of the string provided.N/A
options (optional)An object with values trimStart and trimEnd that represent if the string should only be trimmed from start and/or end respectively.{ trimStart: true, trimEnd: true }

Sample usage

import { trimDelimeters } from 'helper-methods-js'

trimDelimeters("&&*ABC*(&", ["&", "*"]) // outputs "ABC*("