File tree Expand file tree Collapse file tree 5 files changed +55
-1
lines changed
Expand file tree Collapse file tree 5 files changed +55
-1
lines changed Original file line number Diff line number Diff 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 )
359371Filters falsy values of the given array
360372Removes ` null ` and ` undefined ` values and their keys from an object
Original file line number Diff line number Diff line change 2828 " replace" ,
2929 " remove" ,
3030 " deepest" ,
31- " never"
31+ " never" ,
32+ " arrayize"
3233 ],
3334 "repository" : {
3435 "type" : " git" ,
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ export { clone, deepest } from './object';
66export * from './promise' ;
77export { } from './string' ;
88
9+ export * from './arrayize' ;
910export * from './compact' ;
1011export * from './is-falsy' ;
1112export * from './is-truthy' ;
You can’t perform that action at this time.
0 commit comments