-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(provider): moved label splitting code into utility function …
…splitLabels
- Loading branch information
Showing
4 changed files
with
43 additions
and
20 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
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,25 @@ | ||
import { splitLabels } from './label.ts'; | ||
|
||
import { assertEquals } from 'std/assert/assert_equals.ts'; | ||
import { describe, it } from 'std/testing/bdd.ts'; | ||
|
||
import type { FunctionSpec } from './test_spec.ts'; | ||
|
||
describe('GTIN validator', () => { | ||
const passingCases: FunctionSpec<typeof splitLabels> = [ | ||
['single label', 'Nuclear Blast', [{ name: 'Nuclear Blast' }]], | ||
['multiple labels', 'Roc Nation/RocAFella/IDJ', [{ name: 'Roc Nation' }, { name: 'RocAFella' }, { name: 'IDJ' }]], | ||
['multiple labels, trim values', 'Roc Nation / RocAFella / IDJ', [{ name: 'Roc Nation' }, { name: 'RocAFella' }, { | ||
name: 'IDJ', | ||
}]], | ||
['short label', 'A/B', [{ name: 'A/B' }]], | ||
['two labels minimal length', 'ABC/DEF', [{ name: 'ABC' }, { name: 'DEF' }]], | ||
['two labels, one short', 'A/BCD/EFG', [{ name: 'A/BCD' }, { name: 'EFG' }]], | ||
]; | ||
|
||
passingCases.forEach(([description, input, expected]) => { | ||
it(`passes for valid ${description}`, () => { | ||
assertEquals(splitLabels(input), expected); | ||
}); | ||
}); | ||
}); |
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,13 @@ | ||
import { Label } from '@/harmonizer/types.ts'; | ||
|
||
/** | ||
* Split label string using slashes if the results have at least 3 characters. | ||
* | ||
* @param labels String containing one or more label name. | ||
* @returns List of `Label` entries | ||
*/ | ||
export function splitLabels(labels: string): Label[] { | ||
return labels?.split(/(?<=[^/]{3,})\/(?=[^/]{3,})/).map((label) => ({ | ||
name: label.trim(), | ||
})); | ||
} |