Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
666bb42
commit 8989385
Showing
4 changed files
with
108 additions
and
11 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
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,52 @@ | ||
import { fill } from '../src'; | ||
import { createObject, ownPropA, ownPropB } from './tools/create-object'; | ||
import { invalidObjects } from './tools/values'; | ||
|
||
describe('fill method', () => { | ||
|
||
test('should throw on insufficient arguments', () => { | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
expect(() => fill()).toThrow(TypeError); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
expect(() => fill({})).toThrow(TypeError); | ||
|
||
}); | ||
|
||
test('should throw on non object', () => { | ||
|
||
invalidObjects.forEach((object) => { | ||
expect(() => fill(object as never, 0)).toThrow(TypeError); | ||
}); | ||
|
||
}); | ||
|
||
test('should skip prototype properties', () => { | ||
|
||
const instance = createObject(); | ||
|
||
const result = fill(instance, 0); | ||
|
||
expect(result).toEqual({ | ||
[ownPropA]: 0, | ||
[ownPropB]: 0, | ||
}); | ||
|
||
}); | ||
|
||
test('should return a new object', () => { | ||
|
||
const object = { a: 0, b: 0, c: 0, d: 0 }; | ||
|
||
const result = fill(object, 0); | ||
|
||
expect(typeof result).toBe('object'); | ||
expect(result).toEqual(object); | ||
expect(result).not.toBe(object); | ||
|
||
}); | ||
|
||
}); |
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,34 @@ | ||
import { invalidObject, notEnoughArgs } from './errors'; | ||
import hasOwn from './has-own'; | ||
import isObject from './is-object'; | ||
import { Key } from './types'; | ||
|
||
function fill<V, K extends Key, RV = any>( | ||
object: Record<K, V>, | ||
value: RV, | ||
): Record<K, RV> { | ||
|
||
// eslint-disable-next-line prefer-rest-params | ||
const { length: argsLen } = arguments; | ||
|
||
if (argsLen < 2) { | ||
throw notEnoughArgs(argsLen, 2); | ||
} | ||
|
||
if (!isObject(object)) { | ||
throw invalidObject(object); | ||
} | ||
|
||
const result: Record<Key, RV> = {}; | ||
|
||
for (const key in object) { | ||
if (hasOwn.call(object, key)) { | ||
result[key as Key] = value; | ||
} | ||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
export default fill; |
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
export { default as every } from './every'; | ||
export { default as fill } from './fill'; | ||
export { default as filter } from './filter'; | ||
export { default as find } from './find'; | ||
export { default as findKey } from './find-key'; | ||
export { default as forEach } from './for-each'; | ||
export { default as map } from './map'; | ||
export { default as includes } from './includes'; | ||
export { default as keyOf } from './key-of'; | ||
export { default as lastKeyOf } from './last-key-of'; | ||
export { default as includes } from './includes'; | ||
export { default as findKey } from './find-key'; | ||
export { default as find } from './find'; | ||
export { default as filter } from './filter'; | ||
export { default as map } from './map'; | ||
export { default as reduce } from './reduce'; | ||
export { default as some } from './some'; | ||
export { default as every } from './every'; | ||
export { FilterCallback, ForEachCallback, MapCallback, ReduceCallback } from './types'; |