-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
51fdd4c
commit 36b06c7
Showing
5 changed files
with
55 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -28,7 +28,8 @@ | |
"replace", | ||
"remove", | ||
"deepest", | ||
"never" | ||
"never", | ||
"arrayize" | ||
], | ||
"repository": { | ||
"type": "git", | ||
|
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,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']); | ||
}); | ||
}); |
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,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]; | ||
} |
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