Skip to content

Commit

Permalink
feat: created arrayize function
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Nov 25, 2020
1 parent 51fdd4c commit 36b06c7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ limit(str, 3); // 'foo'

### Etc.

#### [Arrayize](https://thalesrc.github.io/js-utils/modules/_arrayize_.html)
Encapsulates a non array value with an array that contains it unless the value is already an array

```typescript
import { arrayize } from "@thalesrc/js-utils";

const foo = 'foo';
const bar = ['bar'];
const fooArr = arrayize(foo); // ['foo'];
const barArr = arrayize(bar); // ['bar'];
```

#### [Compact](https://thalesrc.github.io/js-utils/modules/_compact_.html)
Filters falsy values of the given array
Removes `null` and `undefined` values and their keys from an object
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"replace",
"remove",
"deepest",
"never"
"never",
"arrayize"
],
"repository": {
"type": "git",
Expand Down
21 changes: 21 additions & 0 deletions src/arrayize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'jest';

import { arrayize } from './arrayize';

describe('Arrayize Function', () => {
it('should encapsulate a non array value', () => {
const foo = 'foo';
const bar = {bar: 'bar'};
const baz = null;

expect(arrayize(foo)).toEqual(['foo']);
expect(arrayize(bar)).toEqual([{bar: 'bar'}]);
expect(arrayize(baz)).toEqual([null]);
});

it('should not encapsulate when the value is an array', () => {
const foo = ['foo'];

expect(arrayize(foo)).toEqual(['foo']);
});
});
19 changes: 19 additions & 0 deletions src/arrayize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Encapsulates a non array value with an array that contains it unless the value is already an array
*
* * * *
* Example usage:
* ```typescript
* import { arrayize } from "@thalesrc/js-utils";
*
* const foo = 'foo';
* const bar = ['bar'];
* const fooArr = arrayize(foo); // ['foo'];
* const barArr = arrayize(bar); // ['bar'];
* ```
* * * *
* @param value Array or single value to capsulate
*/
export function arrayize<T>(value: T | T[]): T[] {
return value instanceof Array ? value : [value];
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { clone, deepest } from './object';
export * from './promise';
export { } from './string';

export * from './arrayize';
export * from './compact';
export * from './is-falsy';
export * from './is-truthy';
Expand Down

0 comments on commit 36b06c7

Please sign in to comment.