mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 10:52:11 +00:00
Add objectOmit function
- also bump deps (not parcel because 2 is not the way I want to continue using it) Signed-off-by: Avior <github@avior.me>
This commit is contained in:
parent
ffaf544d10
commit
cb97ded195
@ -1,33 +1,22 @@
|
||||
# URL Manager
|
||||
# Object Util
|
||||
|
||||
simple to use yet powerful Urls parser and formatter
|
||||
Utility functions to manipulate an object
|
||||
|
||||
## Usage
|
||||
|
||||
- Import URLManager
|
||||
- Import Object Util
|
||||
|
||||
```typescript
|
||||
import ObjectUtil from '@dzeio/object-util'
|
||||
import { objectMap, ... } from '@dzeio/object-util'
|
||||
// or
|
||||
const ObjectUtil = require('@dzeio/object-util').default
|
||||
|
||||
// or you can import each funcitons individually
|
||||
const {objectMap, ...} = require('@dzeio/object-util')
|
||||
```
|
||||
|
||||
- or import it from the browser
|
||||
|
||||
```html
|
||||
<script src="https://cdn.jsdelivr.net/npm/@dzeio/object-util@1/dist/browser.js"></script>
|
||||
<!-- It will be available as the same variable -->
|
||||
```
|
||||
|
||||
- Create a new instance
|
||||
|
||||
```typescript
|
||||
// Create a new instance
|
||||
const url = new URLManager() // you can have an URL, URLSearchParams Objects or a string as parameter
|
||||
// or
|
||||
const url = URLManager.fromLocation() // Browser only return a new instance from the current location
|
||||
<!-- each functions will be available as window.{objectMap, ...} or {objectMap, ...}-->
|
||||
```
|
||||
|
||||
- explore !
|
||||
@ -35,14 +24,14 @@ const url = URLManager.fromLocation() // Browser only return a new instance from
|
||||
```typescript
|
||||
|
||||
// Does the same as Array.map
|
||||
objectMap(object, (value, key) => {value + "pouet"})
|
||||
objectMap(object, (value, key) => value + "pouet")
|
||||
|
||||
// does the same as Array.forEach with the addon of stopping if false is returned (like break)
|
||||
// and return if loop was finished or not
|
||||
// does the same as Array.forEach with the addition of stopping if false is returned (like break)
|
||||
// and return if loop was stopped or not
|
||||
objectLoop(object, (value, key) => {})
|
||||
|
||||
// return the values of an object as an array
|
||||
objectToArray(object)
|
||||
objectValues(object)
|
||||
|
||||
// return the keys of an object as an array
|
||||
ObjectKeys(object)
|
||||
@ -61,9 +50,20 @@ objectSort(object, ['key2', 'key1']) // => {key2: value, key1: value, key3: valu
|
||||
cloneObject(object)
|
||||
|
||||
// deeply set an object value while creating empty childs if necessary
|
||||
//ex: this will return {path1, [{path3: 'value'}]} if object is an empty object
|
||||
//ex: this will set {path1, [{path3: 'value'}]} if object is an empty object
|
||||
objectSet(object, ['path1', 0, 'path3'], 'value')
|
||||
|
||||
// deeply compare two objects
|
||||
objectEqual(object, object2)
|
||||
|
||||
// deeply clean an object from undefined, null variables
|
||||
objectClean(object, /* optionnal, defaults */{cleanUndefined: true, cleanNull: false, deep: true})
|
||||
|
||||
// clone (not deeply) an object and remove the keys from the object, 'a' and 'b' for this one
|
||||
objectOmit(object, 'a', 'b')
|
||||
|
||||
// check if a variable is an object
|
||||
isObject(object)
|
||||
```
|
||||
|
||||
_note: with the exception of isObject, every function will throw an error if the object is not an object_
|
||||
|
@ -1,6 +1,6 @@
|
||||
/// <reference types="jest" />
|
||||
|
||||
import { objectSize, objectMap, objectSort, objectEqual, objectKeys, objectSet, objectLoop, objectClone, objectValues, objectClean, isObject } from '../src/ObjectUtil'
|
||||
import { objectSize, objectMap, objectSort, objectEqual, objectKeys, objectSet, objectLoop, objectClone, objectValues, objectClean, isObject, objectOmit } from '../src/ObjectUtil'
|
||||
|
||||
describe('Throw if parameter is not an object', () => {
|
||||
it('should works', () => {
|
||||
@ -252,6 +252,22 @@ describe('Object Clean Tests', () => {
|
||||
objectClean(obj)
|
||||
expect(obj).toEqual({a: '', b: null, d: {da: '', db: null}})
|
||||
})
|
||||
it('should clean deep when set', () => {
|
||||
const obj = {a: '', b: null, c: undefined, d: {da: '', db: null, dc: undefined}}
|
||||
objectClean(obj, {deep: true})
|
||||
expect(obj).toEqual({a: '', b: null, d: {da: '', db: null}})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Object Omit Tests', () => {
|
||||
it('should omit certain elements', () => {
|
||||
const obj = {a: 'a', b: 'c', c: 'b'}
|
||||
expect(objectOmit(obj, 'b')).toEqual({a: 'a', c: 'b'})
|
||||
})
|
||||
it('should not care when key to omit is not present', () => {
|
||||
const obj = {a: 'a', b: 'c', c: 'b'}
|
||||
expect(objectOmit(obj, 'b', 'd')).toEqual({a: 'a', c: 'b'})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Is Object Tests', () => {
|
||||
|
3755
packages/object-util/package-lock.json
generated
3755
packages/object-util/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,22 +1,23 @@
|
||||
{
|
||||
"name": "@dzeio/object-util",
|
||||
"version": "1.3.0",
|
||||
"description": "some Function to make work on objects easier",
|
||||
"description": "Utility functions to manipulate an object",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/dzeiocom/libs.git",
|
||||
"directory": "packages/object-util"
|
||||
},
|
||||
"homepage": "https://github.com/dzeiocom/libs/tree/master/packages/object-util",
|
||||
"author": "Aviortheking",
|
||||
"license": "MIT",
|
||||
"main": "./dist/ObjectUtil.js",
|
||||
"types": "./dist/ObjectUtil.d.ts",
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.2.12",
|
||||
"@types/jest": "^26.0.10",
|
||||
"jest": "^26.4.2",
|
||||
"@types/jest": "^27.0.0",
|
||||
"jest": "^27.0.0",
|
||||
"parcel": "1.12.3",
|
||||
"ts-node": "^9.0.0",
|
||||
"ts-jest": "^27.0.0",
|
||||
"ts-node": "^10.0.0",
|
||||
"typescript": "^4.0.2"
|
||||
},
|
||||
"scripts": {
|
||||
|
@ -39,7 +39,7 @@ export function objectLoop<T = any>(
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an object to an array removing the keys
|
||||
* Transform an object to an array of its values
|
||||
* @param obj the object to transform
|
||||
*/
|
||||
export function objectValues<T = any>(obj: Record<string, T>): Array<T> {
|
||||
@ -56,7 +56,7 @@ export function objectToArray<T = any>(obj: Record<string, T>): Array<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* return the keys of th object
|
||||
* return the keys of the object
|
||||
* @param obj the object
|
||||
*/
|
||||
export function objectKeys(obj: Record<string, any>): Array<string> {
|
||||
@ -69,7 +69,6 @@ export function objectKeys(obj: Record<string, any>): Array<string> {
|
||||
* @param obj the object
|
||||
*/
|
||||
export function objectSize(obj: Record<string, any>): number {
|
||||
mustBeObject(obj)
|
||||
return objectKeys(obj).length
|
||||
}
|
||||
|
||||
@ -102,13 +101,13 @@ export function objectSort<T extends Record<string, any> = Record<string, any>>(
|
||||
* @deprecated use `objectClone`
|
||||
*/
|
||||
export function cloneObject<T = Record<string, any>>(obj: T): T {
|
||||
mustBeObject(obj)
|
||||
return objectClone(obj)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deeply clone an object
|
||||
* @param obj the object to clone
|
||||
* @returns the clone of the object
|
||||
*/
|
||||
export function objectClone<T = Record<string, any>>(obj: T): T {
|
||||
mustBeObject(obj)
|
||||
@ -199,9 +198,11 @@ export function objectEqual(x: Record<string, any>, y: Record<string, any>): boo
|
||||
}
|
||||
|
||||
/**
|
||||
* deeply compare objects and return if they are equal or not
|
||||
* @param x the first object
|
||||
* @param y the second object
|
||||
* deeply clean an object from having {key: undefined}
|
||||
* @param obj the object to clean
|
||||
* @param {boolean?} options.cleanUndefined (default: true) clean undefined from the object
|
||||
* @param {boolean?} options.cleanNull clean null frrom the object
|
||||
* @param {boolean?} options.deep (default: true) deeply clean the object
|
||||
*/
|
||||
export function objectClean(obj: Record<string, any>, options?: {cleanUndefined?: boolean, cleanNull?: boolean, deep?: boolean}): void {
|
||||
mustBeObject(obj)
|
||||
@ -220,10 +221,38 @@ export function objectClean(obj: Record<string, any>, options?: {cleanUndefined?
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* clone the object (not deeply) and emit some keys from cloning
|
||||
* @param obj the object to clone
|
||||
* @param keys the keys to emit
|
||||
* @returns the cloned object
|
||||
*/
|
||||
export function objectOmit<T extends Record<string, any> = Record<string, any>>(obj: T, ...keys: Array<string>): T {
|
||||
const cloned = obj
|
||||
for (const key of keys) {
|
||||
if (key in cloned) {
|
||||
delete cloned[key]
|
||||
}
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
/**
|
||||
* return if an item is an object
|
||||
* @param item the item to check
|
||||
* @returns {boolean} the item is an object
|
||||
*/
|
||||
export function isObject(item: any): item is Record<any, any> {
|
||||
return typeof item === 'object' && item !== null
|
||||
}
|
||||
|
||||
/**
|
||||
* Strict check for an object
|
||||
*
|
||||
* @internal
|
||||
* @param item the item to check
|
||||
* @returns {boolean} throw an error is the item is not an item
|
||||
*/
|
||||
function mustBeObject(item: any): item is Record<any, any> {
|
||||
if (isObject(item)) {
|
||||
return true
|
||||
@ -244,5 +273,6 @@ export default {
|
||||
objectSet,
|
||||
objectEqual,
|
||||
objectClean,
|
||||
objectOmit,
|
||||
isObject
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user