Skip to content

Commit

Permalink
feat: Implement "fill" method
Browse files Browse the repository at this point in the history
  • Loading branch information
manferlo81 committed May 13, 2021
1 parent 666bb42 commit 8989385
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 11 deletions.
20 changes: 15 additions & 5 deletions README.md
Expand Up @@ -68,7 +68,7 @@ console.log(result);
}
```

*See the [API section](#api) for mre details.*
*See the [API section](#api) for more details.*

### Node.js

Expand All @@ -89,7 +89,7 @@ eachProp.forEach(object, callback);

### forEach

*Similar to* `Array.prototype.forEach`*. It calls the provided callback function for every* `key-value-pair` *in the object. Once iniciated there is no way to stop the execution of this function, if you intend to stop the iteration at some point have a look at* [`findKey`](#findkey) *method.*
*Similar to* `Array.prototype.forEach`*. It calls the provided callback function for every* `key-value-pair` *in the object. Once initiated there is no way to stop the execution of this function, if you intend to stop the iteration at some point have a look at* [`findKey`](#findkey) *method.*

```typescript
forEach(object, function callback(value, key, ...extra) => void, ...extra): void;
Expand Down Expand Up @@ -249,7 +249,7 @@ some(object, function callback(value, key, ...extra) => any, ...extra): boolean;
*The callback function inherits the* `this` *value from the function call, so if you want a specific* `this` *value in your callback function, you can call it using the* `call` *method of the* `Function.prototype`*.*
```javascript
some.call(thisrArg, object, callback, ...extra): boolean;
some.call(thisArg, object, callback, ...extra): boolean;
```
### every
Expand All @@ -267,9 +267,19 @@ every(object, function callback(value, key, ...extra) => any, ...extra): boolean
*The callback function inherits the* `this` *value from the function call, so if you want a specific* `this` *value in your callback function, you can call it using the* `call` *method of the* `Function.prototype`*.*
```javascript
every.call(thisrArg, object, callback, ...extra): boolean;
every.call(thisArg, object, callback, ...extra): boolean;
```
### fill
*added in:* `v2.1.0`
*Similar to* `Array.prototype.fill` *with a difference, it returns a new object instead of modifying the given one. Every property in the new object will be set to the provided value.*
```typescript
fill(object, value): object;
```
## License
[MIT](LICENSE) © [Manuel Fernández](https://github.com/manferlo81)
[MIT](LICENSE) © 2019 [Manuel Fernández](https://github.com/manferlo81)
52 changes: 52 additions & 0 deletions __test__/fill.test.ts
@@ -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);

});

});
34 changes: 34 additions & 0 deletions src/fill.ts
@@ -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;
13 changes: 7 additions & 6 deletions src/index.ts
@@ -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';

0 comments on commit 8989385

Please sign in to comment.