Skip to content

Commit 36b06c7

Browse files
feat: created arrayize function
1 parent 51fdd4c commit 36b06c7

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,18 @@ limit(str, 3); // 'foo'
355355

356356
### Etc.
357357

358+
#### [Arrayize](https://thalesrc.github.io/js-utils/modules/_arrayize_.html)
359+
Encapsulates a non array value with an array that contains it unless the value is already an array
360+
361+
```typescript
362+
import { arrayize } from "@thalesrc/js-utils";
363+
364+
const foo = 'foo';
365+
const bar = ['bar'];
366+
const fooArr = arrayize(foo); // ['foo'];
367+
const barArr = arrayize(bar); // ['bar'];
368+
```
369+
358370
#### [Compact](https://thalesrc.github.io/js-utils/modules/_compact_.html)
359371
Filters falsy values of the given array
360372
Removes `null` and `undefined` values and their keys from an object

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"replace",
2929
"remove",
3030
"deepest",
31-
"never"
31+
"never",
32+
"arrayize"
3233
],
3334
"repository": {
3435
"type": "git",

src/arrayize.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'jest';
2+
3+
import { arrayize } from './arrayize';
4+
5+
describe('Arrayize Function', () => {
6+
it('should encapsulate a non array value', () => {
7+
const foo = 'foo';
8+
const bar = {bar: 'bar'};
9+
const baz = null;
10+
11+
expect(arrayize(foo)).toEqual(['foo']);
12+
expect(arrayize(bar)).toEqual([{bar: 'bar'}]);
13+
expect(arrayize(baz)).toEqual([null]);
14+
});
15+
16+
it('should not encapsulate when the value is an array', () => {
17+
const foo = ['foo'];
18+
19+
expect(arrayize(foo)).toEqual(['foo']);
20+
});
21+
});

src/arrayize.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Encapsulates a non array value with an array that contains it unless the value is already an array
3+
*
4+
* * * *
5+
* Example usage:
6+
* ```typescript
7+
* import { arrayize } from "@thalesrc/js-utils";
8+
*
9+
* const foo = 'foo';
10+
* const bar = ['bar'];
11+
* const fooArr = arrayize(foo); // ['foo'];
12+
* const barArr = arrayize(bar); // ['bar'];
13+
* ```
14+
* * * *
15+
* @param value Array or single value to capsulate
16+
*/
17+
export function arrayize<T>(value: T | T[]): T[] {
18+
return value instanceof Array ? value : [value];
19+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export { clone, deepest } from './object';
66
export * from './promise';
77
export { } from './string';
88

9+
export * from './arrayize';
910
export * from './compact';
1011
export * from './is-falsy';
1112
export * from './is-truthy';

0 commit comments

Comments
 (0)