-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
435a9c4
commit b5d37de
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @file Type Tests - shake | ||
* @module tutils/utils/tests/unit-d/shake | ||
*/ | ||
|
||
import type { Shake } from '#src/types' | ||
import type testSubject from '../shake' | ||
|
||
describe('unit-d:utils/shake', () => { | ||
it('should return Shake<T, F>', () => { | ||
// Arrange | ||
type T = { x: number; y: null } | ||
type F = null | ||
type Expect = Shake<T, F> | ||
|
||
// Expect | ||
expectTypeOf<typeof testSubject<T, F>>().returns.toEqualTypeOf<Expect>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @file Unit Tests - shake | ||
* @module tutils/utils/tests/unit/shake | ||
*/ | ||
|
||
import testSubject from '../shake' | ||
|
||
describe('unit:utils/shake', () => { | ||
let obj: { x: number; y: undefined } | ||
|
||
beforeAll(() => { | ||
obj = { x: faker.number.int(), y: undefined } | ||
}) | ||
|
||
it('should return filtered object', () => { | ||
// Act | ||
const result = testSubject(obj) | ||
|
||
// Expect | ||
expect(result).to.equal(obj).and.have.property('x', obj.x) | ||
expect(result).to.not.have.property('y') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @file Utilities - shake | ||
* @module tutils/utils/shake | ||
*/ | ||
|
||
import type { Fn, ObjectCurly, Shake, Values } from '#src/types' | ||
import cast from './cast' | ||
import isUndefined from './is-undefined' | ||
import properties from './properties' | ||
|
||
/** | ||
* Remove properties from an object where the key-value meets a given `filter` | ||
* condition. The initial target object **will** be modified. | ||
* | ||
* All `undefined` properties will be removed if a `filter` is not provided. | ||
* | ||
* Inherited properties will not be removed. | ||
* | ||
* **Note**: TypeScript does not track inheritance. The return type may differ | ||
* from the actual return value when target objects contain inherited properties | ||
* (e.g. `Map`, `Set`) that meet the `filter` condition. In such cases, the | ||
* return type will have less properties than present on the return value. | ||
* | ||
* @see {@linkcode Shake} | ||
* | ||
* @todo examples | ||
* | ||
* @template T - Object to filter | ||
* @template F - Key value filter | ||
* | ||
* @param {T} obj - Object to filter | ||
* @param {Fn<[Values<T>[number]], boolean>} [filter=isUndefined] - Value filter | ||
* @return {Shake<T, F>} Filtered object | ||
*/ | ||
const shake = <T extends ObjectCurly, F = undefined>( | ||
obj: T, | ||
filter: Fn<[Values<T>[number]], boolean> = isUndefined | ||
): Shake<T, F> => { | ||
return properties(obj).reduce<Shake<T, F>>((acc, key) => { | ||
filter(obj[cast<keyof typeof obj>(key)]) && Reflect.deleteProperty(acc, key) | ||
return acc | ||
}, cast(obj)) | ||
} | ||
|
||
export default shake |