Skip to content

Commit

Permalink
Merge pull request #8 from vigan-abd/chore/merge-cleanup
Browse files Browse the repository at this point in the history
Chore/merge cleanup
  • Loading branch information
prdn authored Oct 14, 2023
2 parents ef4d705 + 3c270dd commit 914e9c6
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 19 deletions.
22 changes: 22 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### Description:
...

### Breaking changes:
- [ ]

### New features:
- [ ]

### Fixes:
- [ ]

### PR status:
- [ ] Version bumped
- [ ] Change-log updated
- [ ] Tests added or updated
- [ ] PR keeps 100% test coverage
- [ ] Type definition updated
- [ ] Readme updated
- [ ] Linter is applied
- [ ] Const arrow functions are used instead of pure function definitions where applicable
- [ ] Functions are listed in alphabetic order in index.js, index.d.ts and readme
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.2.0
- feat: get
- feat: isEmpty
- feat: merge

# 1.1.0
- feat: cloneDeep

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

Currently supported utils:
- `camelize` - simple camel case
- `cloneDeep` - deep clone functionality for objects
- `get` - get the object members by path
- `getArrayHasIntersect` - checks if arrays have at least one common value
- `getArrayUniq` - gets unique values form array
- `isEmpty` - Checks if value is an empty object, collection, map, or set
- `isEmpty` - checks if value is an empty object, collection, map, or set
- `isNil` - checks whenever value is null or undefined
- `isPlainObject` - checks if input is object, not null object and not array object
- `merge` - deep merge functionality for objects
- `omit` - provides new object that omits only specific fields of source object
- `omitBy` - provides new object that omits only specific fields of source object depending on predicate function filter
- `pick` - provides new object that picks only specific fields of source object
- `pickBy` - provides new object that picks only specific fields of source object depending on predicate function filter
- `camelize` - simple camel case
9 changes: 5 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export function camelize(str: string): string
export function cloneDeep (obj: Object): Object
export function get(obj: Object, path: string | Array<string | number>, defaultValue: any)
export function getArrayHasIntersect (arr1: Array<any>, arr2: Array<any>): boolean
export function getArrayUniq (arr: Array<any>): Array<any>
export function isEmpty(val: any): boolean
export function isNil(val: any): boolean
export function isPlainObject(val: any): boolean
export function pick(obj: Object, keys: Array<string>): Object
export function pickBy(obj: Object, predicate: (val: any, key: string) => boolean): Object
export function camelize(str: string): string
export function merge(obj: Object, ...sources: Object[]): Object
export function omit(obj: Object, keys: Array<string>): Object
export function omitBy(obj: Object, predicate: (val: any, key: string) => boolean): Object
export function merge(obj: Object, ...sources: Object): Object
export function pick(obj: Object, keys: Array<string>): Object
export function pickBy(obj: Object, predicate: (val: any, key: string) => boolean): Object
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
'use strict'

const camelize = require('./src/camelize')
const cloneDeep = require('./src/cloneDeep')
const get = require('./src/get')
const getArrayHasIntersect = require('./src/getArrayHasIntersect')
const getArrayUniq = require('./src/getArrayUniq')
const isEmpty = require('./src/isEmpty')
const isNil = require('./src/isNil')
const isPlainObject = require('./src/isPlainObject')
const pick = require('./src/pick')
const pickBy = require('./src/pickBy')
const merge = require('./src/merge')
const omit = require('./src/omit')
const omitBy = require('./src/omitBy')
const camelize = require('./src/camelize')
const merge = require('./src/merge')
const pick = require('./src/pick')
const pickBy = require('./src/pickBy')

module.exports = {
camelize,
cloneDeep,
get,
getArrayHasIntersect,
getArrayUniq,
isEmpty,
isNil,
isPlainObject,
pick,
pickBy,
camelize,
merge,
omit,
omitBy,
merge
pick,
pickBy
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinex/lib-js-util-base",
"version": "1.1.0",
"version": "1.2.0",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
49 changes: 49 additions & 0 deletions src/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

/**
*
* @param {string} path
* @returns {Array<string>}
*/
const pathToArray = (path) => {
if (Array.isArray(path)) return path
return path.split(/\[|\]\.|\]\[|\.|\]/gm).filter((key) => key !== '')
}

/**
*
* @param {any} obj
* @param {Array<string>} nextPath
* @returns {any}
*/
const getDeeperProp = (obj, nextPath, defaults) => {
const key = nextPath.shift()
if (obj[key] !== undefined) {
if (nextPath.length > 0) return getDeeperProp(obj[key], nextPath, defaults)

return obj[key]
}

return defaults
}

/**
*
* @param {any} object
* @param {string | Array} path
* @param {any} defaults
* @returns {any}
*/
const get = (object, path, defaults) => {
if (typeof object !== 'object' || object === null) {
return defaults
}

if (typeof path === 'string' && object[path] !== undefined) {
return object[path]
}

return getDeeperProp(object, pathToArray(path), defaults)
}

module.exports = get
8 changes: 4 additions & 4 deletions test/camelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ const assert = require('assert')
const { camelize } = require('../index')

describe('camelize', () => {
it('should convert strings to camelCase', function () {
it('should convert strings to camelCase', () => {
assert.strictEqual(camelize('hello_world'), 'helloWorld')
assert.strictEqual(camelize('Hello-world'), 'helloWorld')
assert.strictEqual(camelize('hello world'), 'helloWorld')
})

it('should handle single words', function () {
it('should handle single words', () => {
assert.strictEqual(camelize('hello'), 'hello')
assert.strictEqual(camelize('Hello'), 'hello')
})

it('should handle empty strings', function () {
it('should handle empty strings', () => {
assert.strictEqual(camelize(''), '')
})

it('should handle strings with numbers', function () {
it('should handle strings with numbers', () => {
assert.strictEqual(camelize('hello1_world2'), 'hello1World2')
})
})
84 changes: 84 additions & 0 deletions test/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict'

/* eslint-env mocha */

const assert = require('assert')
const get = require('../src/get')

describe('get', () => {
it('should get property from object', () => {
const object = { a: 'foo' }
const res = get(object, 'a')
assert.strictEqual(res, 'foo')
})

it('should get property from nested object', () => {
const object = { a: { foo: { bar: 'baz' } } }
const res = get(object, 'a.foo.bar')
assert.strictEqual(res, 'baz')
})

it('should get property by name passed in square brackets', () => {
const object = { a: { foo: { bar: 'baz' } } }
const res = get(object, 'a.foo[bar]')
assert.strictEqual(res, 'baz')
})

it('should get property in array', () => {
const object = { a: [1, 2, { foo: { bar: 'baz' } }] }
const res = get(object, 'a[2].foo[bar]')
assert.strictEqual(res, 'baz')
})

it('should get property in array at the lowest level', () => {
const object = { a: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }
const res = get(object, 'a[2].foo.bar.[3]')
assert.strictEqual(res, 'baz')
})

it('should get property if the first level is array', () => {
const object = [{ a: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }]
const res = get(object, '[0]a[2].foo.bar.[3]')
assert.strictEqual(res, 'baz')
})

it('should also works if path passed as array', () => {
const object = [{ a: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }]
const res = get(object, [0, 'a', '2', 'foo', 'bar', 3])
assert.strictEqual(res, 'baz')
})

it('should work with "false" values as part of path', () => {
const object = [{ false: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }]
const res = get(object, '[0]false[2].foo.bar.[3]')
assert.strictEqual(res, 'baz')
})

it('should work with nullable values', () => {
const object = [{ false: [1, 2, { foo: { bar: [0, 1, 2, { null: 'baz' }] } }] }]
const res = get(object, '[0]false[2].foo.bar.[3]null')
assert.strictEqual(res, 'baz')
})

it('should return default value if item is not found by path', () => {
const object = [{ false: [1, 2, { foo: { bar: [0, 1, 2, 'baz'] } }] }]
const res = get(object, '[0]false[4].foo.bar.[5]', 'some default value')
assert.strictEqual(res, 'some default value')
})

it('should return default value if result item is not defined', () => {
const object = [{ false: [1, 2, { foo: { bar: [0, 1] } }] }]
const res = get(object, '[0]false[2].foo.bar.[2]', 'some default value')
assert.strictEqual(res, 'some default value')
})

it('should return default value if input is null', () => {
const res = get(null, 'test', 'some default value')
assert.strictEqual(res, 'some default value')
})

it('should return default value if input is not object', () => {
const res = get(true, 'test', 'some default value')
assert.strictEqual(res, 'some default value')
})
})

0 comments on commit 914e9c6

Please sign in to comment.