Skip to content

Commit

Permalink
Merge pull request #34 from dictor93/feat/lodash
Browse files Browse the repository at this point in the history
Feature. Lodash functions migration
  • Loading branch information
vigan-abd authored Nov 18, 2024
2 parents a17a181 + 1914ff1 commit 7b7b3b8
Show file tree
Hide file tree
Showing 25 changed files with 568 additions and 3 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#1.17.0
- feat: assignWith
- feat: clone
- feat: findLastIndex
- feat: isMatch
- feat: isObjectLike
- feat: mean
- feat: union
- feat: uniqWith
- feat: update

#1.16.0
- feat: regex validations

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
General node js utils library.

Currently supported utils:
- `assignWith` - Assigns the object properties from the sources
- `camelize` - simple camel case
- `capitalize` - make first char uppercase
- `clone` - Shallow clone the object
- `cloneDeep` - deep clone functionality for objects
- `difference` - creates an array of values from the first argument not included in the second argument
- `findLastIndex` - Returns the index of the last element in the array that satisfies the provided testing function.
- `flow` - generate a composite function that returns the result of provided functions called by the chain; each previous function result passes as the argument of the next function in the chain
- `freezeDeep` - deep freezes objects
- `get` - get the object members by path
Expand All @@ -18,14 +21,17 @@ Currently supported utils:
- `isEqual` - check if passed two values are equal
- `isFinite` - checks if input is a finite number.
- `isFunction` - checks if input is a Function object.
- `isMatch` - Checks if first argument is a match with last
- `isNil` - checks whenever value is null or undefined
- `isNumber` - checks if input is a number.
- `isObject` - checks if the input is not a nullable object instance
- `isObjectLike` - Checks if passed `value` is object-like. A value is object-like if it's not `null`
- `isPlainObject` - checks if input is object, not null object and not array object
- `isString` - checks if input is a string
- `mapKeys` - creates new object with the same values but with keys mapped by the provided function
- `mapValues` - Maps the values of an object or array using the provided iteratee function or property path
- `max` - computes the maximum value of array. If array is empty or falsey, undefined is returned
- `mean` - Returns the mean of an array of numbers
- `merge` - deep merge functionality for objects
- `min` - computes the minimum value of array. If array is empty or falsey, undefined is returned
- `omit` - provides new object that omits only specific fields of source object
Expand All @@ -37,6 +43,9 @@ Currently supported utils:
- `snakeCase` - convert camel case and string/dash separated strings to snake case
- `sum` - calculate sum of array items
- `sumBy` - calculate sum of array items using iteratee function or string shortcut
- `union` - Returns the union of the given arrays
- `uniqBy` - get unique values of array by the iteratee function or property path
- `uniqWith` - Returns a new array with unique values, using a comparator function
- `update` - Update the object with the updater by path
- `validateInput` - validates the input based on the regex format options: `NUMBER | EMAIL | PATH | NAME | NAME_WITH_DIGITS | INPUT | ADDRESS | PHONE_CODE | PHONE | IMAGE | FILE | FILENAME | PASSWORD`
- `without` - creates an array of values from the first argument excluding all given arguments
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export function assignInWith (obj: Object, ...sources: Object[]): Object
export function camelize (str: string): string
export function capitalize (str: string): string
export function clone (obj: Object): Object
export function cloneDeep (obj: Object): Object
export function difference (array: Array<any>, values: Array<any>): Array<any>
export function findLastIndex (array: Array<any>, predicate: Function): number
export function flow (funcs: Array<Function>): Function
export function freezeDeep (obj: Object): Object
export function get (obj: Object, path: string | Array<string | number>, defaultValue: any): any
Expand All @@ -14,14 +17,17 @@ export function isEmpty (val: any): boolean
export function isEqual (value: any, another: any): boolean
export function isFinite (val: any): boolean
export function isFunction (val: any): boolean
export function isMatch (obj: Object, source: Object): boolean
export function isNil (val: any): boolean
export function isNumber (val: any): boolean
export function isObject (verifiable: any): Boolean
export function isObjectLike(value: any): boolean
export function isPlainObject (val: any): boolean
export function isString (val: any): boolean
export function mapKeys(obj: Object, mapper: (val: any, key: string) => string): Object
export function mapValues(obj: Object, mapper: (val: any, key: string) => any): Object
export function max (array: Array<any>): any
export function mean (values: Array<number>): number
export function merge (obj: Object, ...sources: Object[]): Object
export function min (array: Array<any>): any
export function omit (obj: Object, keys: Array<string>): Object
Expand All @@ -33,6 +39,9 @@ export function shuffle<T> (array: Array<T>): Array<T>
export function snakeCase (str: string): string
export function sum (values: Array<string>): number
export function sumBy (values: Array, iteratee: Function | string): number
export function union (...arrays: Array[]): Array
export function uniqBy (array: Array, iteratee: Function | string): Array
export function uniqWith (array: Array, comparator: Function): Array
export function update (obj: Object, path: string | Array<string | number>, updater: Function): Object
export function validateInput ( input: string, format: string ): Boolean
export function without (array: Array, ...values: Array): Array
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict'

const assignInWith = require('./src/assignWith')
const camelize = require('./src/camelize')
const capitalize = require('./src/capitalize')
const clone = require('./src/clone')
const cloneDeep = require('./src/cloneDeep')
const difference = require('./src/difference')
const findLastIndex = require('./src/findLastIndex')
const flow = require('./src/flow')
const freezeDeep = require('./src/freezeDeep')
const get = require('./src/get')
Expand All @@ -16,14 +19,17 @@ const isEmpty = require('./src/isEmpty')
const isEqual = require('./src/isEqual')
const isFinite = require('./src/isFinite')
const isFunction = require('./src/isFunction')
const isMatch = require('./src/isMatch')
const isNil = require('./src/isNil')
const isNumber = require('./src/isNumber')
const isObject = require('./src/isObject')
const isObjectLike = require('./src/isObjectLike')
const isPlainObject = require('./src/isPlainObject')
const isString = require('./src/isString')
const mapKeys = require('./src/mapKeys')
const mapValues = require('./src/mapValues')
const max = require('./src/max')
const mean = require('./src/mean')
const merge = require('./src/merge')
const min = require('./src/min')
const omit = require('./src/omit')
Expand All @@ -35,15 +41,22 @@ const shuffle = require('./src/shuffle')
const snakeCase = require('./src/snakeCase')
const sum = require('./src/sum')
const sumBy = require('./src/sumBy')
const union = require('./src/union')
const uniqBy = require('./src/uniqBy')
const uniqWith = require('./src/uniqWith')
const update = require('./src/update')
const validateInput = require('./src/validateInput')
const without = require('./src/without')

module.exports = {
assignInWith,
camelize,
capitalize,
clone,
cloneDeep,
difference,
findLastIndex,
flow,
freezeDeep,
get,
getArrayHasIntersect,
Expand All @@ -55,15 +68,17 @@ module.exports = {
isEqual,
isFinite,
isFunction,
isMatch,
isNil,
isNumber,
isObject,
isObjectLike,
isPlainObject,
isString,
flow,
mapKeys,
mapValues,
max,
mean,
merge,
min,
omit,
Expand All @@ -75,7 +90,10 @@ module.exports = {
snakeCase,
sum,
sumBy,
union,
uniqBy,
uniqWith,
update,
validateInput,
without
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinexcom/lib-js-util-base",
"version": "1.16.0",
"version": "1.17.0",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 24 additions & 0 deletions src/assignWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

/**
* Assigns the object properties from the sources
*
* @param {object} object
* @param {...(object|Function)} rest
* @returns {object}
*/
const assignInWith = (object, ...rest) => {
if (rest.length === 0) return object
const customizer = rest.pop()
if (typeof customizer !== 'function') return object
if (rest.length === 0) return object
rest.forEach((source) => {
for (const key in source) {
const value = customizer ? customizer(object[key], source[key], key, object, source) : undefined
object[key] = value === undefined ? source[key] : value
}
})
return object
}

module.exports = assignInWith
21 changes: 21 additions & 0 deletions src/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const isObject = require('./isObject')

/**
* Clone the object
*
* @param {any} obj
* @returns {any}
*/
const clone = (obj) => {
if (Array.isArray(obj)) {
return obj.slice()
}
if (isObject(obj)) {
return Object.assign({}, obj)
}
return obj
}

module.exports = clone
22 changes: 22 additions & 0 deletions src/findLastIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

/**
* Returns the index of the last element in the array that satisfies the provided testing function.
*
* @param {Array} array
* @param {Function} predicate
* @param {number} fromIndex
* @returns {number}
*/
const findLastIndex = (array, predicate, fromIndex) => {
let index = fromIndex || array.length - 1
while (index >= 0) {
if (predicate(array[index])) {
return index
}
index--
}
return -1
}

module.exports = findLastIndex
29 changes: 29 additions & 0 deletions src/isMatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

/**
* Checks if `object` is a match with `source`.
*
* @param {Object} object The object to inspect.
* @param {Object} source The object of property values to match.
* @returns {boolean} Returns `true` if `object` is a match, else `false`.
*/
const isMatch = (object, source) => {
if (object === source) return true
if (!object) return false
if (!source) return true
if (typeof source === 'string') {
if (typeof object === 'string') {
return object.slice(0, source.length) === source
}
return false
}
if (Object.keys(source)?.length) {
for (const key in source) {
if (!isMatch(object[key], source[key])) return false
}
return true
}
return false
}

module.exports = isMatch
13 changes: 13 additions & 0 deletions src/isObjectLike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

/**
* Checks if `value` is object-like. A value is object-like if it's not `null`
*
* @param {*} value The value to check
* @returns {boolean} Returns `true` if `value` is object-like, else `false`
*/
const isObjectLike = (value) => {
return typeof value === 'object' && value !== null
}

module.exports = isObjectLike
17 changes: 17 additions & 0 deletions src/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

/**
* Returns the mean of an array of numbers.
*
* @param {Array<number>} arr
* @returns {number}
*/
const mean = (arr) => {
const length = arr ? arr.length : 0
if (length === 0) {
return NaN
}
return arr.reduce((acc, val) => +acc + +val, 0) / arr.length
}

module.exports = mean
22 changes: 22 additions & 0 deletions src/union.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

/**
* Returns the union of the given arrays.
*
* @param {...Array} arrays The arrays to inspect.
* @returns {Array} Returns the new array of combined values.
*/
const union = (...arrays) => {
if (arrays.length === 0) return []
const result = []
arrays.forEach((array) => {
Array.isArray(array) && array.forEach((value) => {
if (!result.includes(value)) {
result.push(value)
}
})
})
return result
}

module.exports = union
21 changes: 21 additions & 0 deletions src/uniqWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'
/**
* Returns a new array with unique values, using a comparator function.
*
* @param {Array} array The array to inspect.
* @param {Function} comparator The comparator function.
* @returns {Array} Returns the new array of filtered values.
*/
const uniqWith = (array, comparator) => {
if (!Array.isArray(array)) {
return []
}
if (typeof comparator !== 'function') {
return array
}
return array.filter((item, index) => {
return array.findIndex((other) => comparator(item, other)) === index
})
}

module.exports = uniqWith
29 changes: 29 additions & 0 deletions src/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const pathToArray = require('./util/pathToArray')

/**
* Update the object with the updater by path
*
* @param {object} object
* @param {string} path
* @param {function} updater
*/
const update = (object, path, updater) => {
if (!object) return
if (!path) return
if (!updater) return
const pathArray = pathToArray(path)
pathArray.reduce((parent, key, index) => {
if (index === pathArray.length - 1) {
parent[key] = updater(parent[key])
return parent
}
if (!parent[key]) {
parent[key] = {}
}
return parent[key]
}, object)
}

module.exports = update
Loading

0 comments on commit 7b7b3b8

Please sign in to comment.