-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): parse tags array from string
Parse tags array from string if not already an array. it is possible for the tags array to actually be a string. In the case of being passed from the command line tool `--dockerTags=x` yargs will only coerce to an array if the flag is specified multiple times. This keeps the functionality the same regaurdless of how it is passed
- Loading branch information
1 parent
07f2063
commit 27d078b
Showing
5 changed files
with
53 additions
and
3 deletions.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
toArray: require('./to-array.js') | ||
} |
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,9 @@ | ||
'use strict' | ||
|
||
const CSV_SEP_EXP = /\s*,\s*/ | ||
module.exports = function toArray(item, sep = CSV_SEP_EXP) { | ||
if (!item) return [] | ||
if (item instanceof Set) return Array.from(item) | ||
if (Array.isArray(item)) return item | ||
return typeof item === 'string' ? item.split(sep) : [item] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
const {test, threw} = require('tap') | ||
const array = require('../../../lib/lang/array/index.js') | ||
|
||
test('array', async (t) => { | ||
t.test('toArray', async (t) => { | ||
const cases = [ | ||
{value: undefined, expected: [], message: 'toArray(undefined) == []'} | ||
, {value: null, expected: [], message: 'toArray(null) == []'} | ||
, {value: 1, expected: [1], message: 'toArray(1) == [1]'} | ||
, {value: '', expected: [], message: 'toArray(\'\') == []'} | ||
, {value: 'test', expected: ['test']} | ||
, {value: '1,2,3', expected: ['1', '2', '3']} | ||
, {value: '1, 2, 3', expected: ['1', '2', '3']} | ||
, {value: '1, 2, 3', expected: ['1', ' 2', ' 3'], sep: ','} | ||
, {value: '1|2|3', expected: ['1', '2', '3'], sep: '|'} | ||
, {value: [1, 2, 3], expected: [1, 2, 3]} | ||
, {value: new Set([1, null, 'test']), expected: [1, null, 'test']} | ||
] | ||
for (const current of cases) { | ||
const args = [current.value] | ||
if (current.sep) { | ||
args.push(current.sep) | ||
} | ||
|
||
t.deepEqual( | ||
array.toArray(...args) | ||
, current.expected | ||
, current.message || `toArray(${current.value}) == ${current.expected}` | ||
) | ||
} | ||
}) | ||
}).catch(threw) |