1
0
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:
Florian Bouillon 2021-09-28 11:47:33 +02:00
parent ffaf544d10
commit cb97ded195
5 changed files with 1467 additions and 2407 deletions

View File

@ -1,33 +1,22 @@
# URL Manager # Object Util
simple to use yet powerful Urls parser and formatter Utility functions to manipulate an object
## Usage ## Usage
- Import URLManager - Import Object Util
```typescript ```typescript
import ObjectUtil from '@dzeio/object-util' import { objectMap, ... } from '@dzeio/object-util'
// or // or
const ObjectUtil = require('@dzeio/object-util').default const {objectMap, ...} = require('@dzeio/object-util')
// or you can import each funcitons individually
``` ```
- or import it from the browser - or import it from the browser
```html ```html
<script src="https://cdn.jsdelivr.net/npm/@dzeio/object-util@1/dist/browser.js"></script> <script src="https://cdn.jsdelivr.net/npm/@dzeio/object-util@1/dist/browser.js"></script>
<!-- It will be available as the same variable --> <!-- each functions will be available as window.{objectMap, ...} or {objectMap, ...}-->
```
- 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
``` ```
- explore ! - explore !
@ -35,14 +24,14 @@ const url = URLManager.fromLocation() // Browser only return a new instance from
```typescript ```typescript
// Does the same as Array.map // 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) // does the same as Array.forEach with the addition of stopping if false is returned (like break)
// and return if loop was finished or not // and return if loop was stopped or not
objectLoop(object, (value, key) => {}) objectLoop(object, (value, key) => {})
// return the values of an object as an array // return the values of an object as an array
objectToArray(object) objectValues(object)
// return the keys of an object as an array // return the keys of an object as an array
ObjectKeys(object) ObjectKeys(object)
@ -61,9 +50,20 @@ objectSort(object, ['key2', 'key1']) // => {key2: value, key1: value, key3: valu
cloneObject(object) cloneObject(object)
// deeply set an object value while creating empty childs if necessary // 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') objectSet(object, ['path1', 0, 'path3'], 'value')
// deeply compare two objects // deeply compare two objects
objectEqual(object, object2) 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_

View File

@ -1,6 +1,6 @@
/// <reference types="jest" /> /// <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', () => { describe('Throw if parameter is not an object', () => {
it('should works', () => { it('should works', () => {
@ -252,6 +252,22 @@ describe('Object Clean Tests', () => {
objectClean(obj) objectClean(obj)
expect(obj).toEqual({a: '', b: null, d: {da: '', db: null}}) 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', () => { describe('Is Object Tests', () => {

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +1,23 @@
{ {
"name": "@dzeio/object-util", "name": "@dzeio/object-util",
"version": "1.3.0", "version": "1.3.0",
"description": "some Function to make work on objects easier", "description": "Utility functions to manipulate an object",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/dzeiocom/libs.git", "url": "https://github.com/dzeiocom/libs.git",
"directory": "packages/object-util" "directory": "packages/object-util"
}, },
"homepage": "https://github.com/dzeiocom/libs/tree/master/packages/object-util",
"author": "Aviortheking", "author": "Aviortheking",
"license": "MIT", "license": "MIT",
"main": "./dist/ObjectUtil.js", "main": "./dist/ObjectUtil.js",
"types": "./dist/ObjectUtil.d.ts", "types": "./dist/ObjectUtil.d.ts",
"devDependencies": { "devDependencies": {
"@types/chai": "^4.2.12", "@types/jest": "^27.0.0",
"@types/jest": "^26.0.10", "jest": "^27.0.0",
"jest": "^26.4.2",
"parcel": "1.12.3", "parcel": "1.12.3",
"ts-node": "^9.0.0", "ts-jest": "^27.0.0",
"ts-node": "^10.0.0",
"typescript": "^4.0.2" "typescript": "^4.0.2"
}, },
"scripts": { "scripts": {

View File

@ -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 * @param obj the object to transform
*/ */
export function objectValues<T = any>(obj: Record<string, T>): Array<T> { 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 * @param obj the object
*/ */
export function objectKeys(obj: Record<string, any>): Array<string> { 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 * @param obj the object
*/ */
export function objectSize(obj: Record<string, any>): number { export function objectSize(obj: Record<string, any>): number {
mustBeObject(obj)
return objectKeys(obj).length return objectKeys(obj).length
} }
@ -102,13 +101,13 @@ export function objectSort<T extends Record<string, any> = Record<string, any>>(
* @deprecated use `objectClone` * @deprecated use `objectClone`
*/ */
export function cloneObject<T = Record<string, any>>(obj: T): T { export function cloneObject<T = Record<string, any>>(obj: T): T {
mustBeObject(obj)
return objectClone(obj) return objectClone(obj)
} }
/** /**
* Deeply clone an object * Deeply clone an object
* @param obj the object to clone * @param obj the object to clone
* @returns the clone of the object
*/ */
export function objectClone<T = Record<string, any>>(obj: T): T { export function objectClone<T = Record<string, any>>(obj: T): T {
mustBeObject(obj) 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 * deeply clean an object from having {key: undefined}
* @param x the first object * @param obj the object to clean
* @param y the second object * @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 { export function objectClean(obj: Record<string, any>, options?: {cleanUndefined?: boolean, cleanNull?: boolean, deep?: boolean}): void {
mustBeObject(obj) 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> { export function isObject(item: any): item is Record<any, any> {
return typeof item === 'object' && item !== null 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> { function mustBeObject(item: any): item is Record<any, any> {
if (isObject(item)) { if (isObject(item)) {
return true return true
@ -244,5 +273,6 @@ export default {
objectSet, objectSet,
objectEqual, objectEqual,
objectClean, objectClean,
objectOmit,
isObject isObject
} }