@internetarchive/field-parsers
Advanced tools
Comparing version 0.1.2-alpha.2 to 0.1.2
import { FieldParserInterface, FieldParserRawValue } from '../field-parser-interface'; | ||
export declare class ListParser<T> implements FieldParserInterface<T[]> { | ||
private parser; | ||
constructor(parser: FieldParserInterface<T>); | ||
private separators; | ||
constructor(parser: FieldParserInterface<T>, options?: { | ||
separators?: string[]; | ||
}); | ||
/** @inheritdoc */ | ||
@@ -6,0 +9,0 @@ parseValue(rawValue: FieldParserRawValue): T[]; |
export class ListParser { | ||
constructor(parser) { | ||
constructor(parser, options) { | ||
this.separators = [',', ';']; | ||
this.parser = parser; | ||
if (options && options.separators) { | ||
this.separators = options.separators; | ||
} | ||
} | ||
@@ -9,6 +13,7 @@ /** @inheritdoc */ | ||
let results = []; | ||
// first try splitting by comma, then by semi-colon | ||
results = stringifiedValue.split(','); | ||
if (results.length === 1) | ||
results = stringifiedValue.split(';'); | ||
for (const separator of this.separators) { | ||
results = stringifiedValue.split(separator); | ||
if (results.length > 1) | ||
break; | ||
} | ||
return this.parseListValues(results); | ||
@@ -15,0 +20,0 @@ } |
@@ -55,3 +55,15 @@ import { expect } from '@open-wc/testing'; | ||
}); | ||
it('can parse a list of strings with custom separator', async () => { | ||
const stringParser = new StringParser(); | ||
const parser = new ListParser(stringParser, { separators: ['-'] }); | ||
const response = parser.parseValue('boop - bop - beep'); | ||
expect(response).to.deep.equal(['boop', 'bop', 'beep']); | ||
}); | ||
it('can parse a list of strings with the second custom separator', async () => { | ||
const stringParser = new StringParser(); | ||
const parser = new ListParser(stringParser, { separators: ['-', '|'] }); | ||
const response = parser.parseValue('boop | bop | beep'); | ||
expect(response).to.deep.equal(['boop', 'bop', 'beep']); | ||
}); | ||
}); | ||
//# sourceMappingURL=list.test.js.map |
{ | ||
"version": "0.1.2-alpha.2", | ||
"version": "0.1.2", | ||
"name": "@internetarchive/field-parsers", | ||
@@ -4,0 +4,0 @@ "description": "Field parsers for Internet Archive metadata fields.", |
@@ -9,4 +9,14 @@ import { | ||
constructor(parser: FieldParserInterface<T>) { | ||
private separators = [',', ';']; | ||
constructor( | ||
parser: FieldParserInterface<T>, | ||
options?: { | ||
separators?: string[]; | ||
} | ||
) { | ||
this.parser = parser; | ||
if (options && options.separators) { | ||
this.separators = options.separators; | ||
} | ||
} | ||
@@ -18,5 +28,8 @@ | ||
let results: string[] = []; | ||
// first try splitting by comma, then by semi-colon | ||
results = stringifiedValue.split(','); | ||
if (results.length === 1) results = stringifiedValue.split(';'); | ||
for (const separator of this.separators) { | ||
results = stringifiedValue.split(separator); | ||
if (results.length > 1) break; | ||
} | ||
return this.parseListValues(results); | ||
@@ -23,0 +36,0 @@ } |
@@ -63,2 +63,16 @@ import { expect } from '@open-wc/testing'; | ||
}); | ||
it('can parse a list of strings with custom separator', async () => { | ||
const stringParser = new StringParser(); | ||
const parser = new ListParser(stringParser, { separators: ['-'] }); | ||
const response = parser.parseValue('boop - bop - beep'); | ||
expect(response).to.deep.equal(['boop', 'bop', 'beep']); | ||
}); | ||
it('can parse a list of strings with the second custom separator', async () => { | ||
const stringParser = new StringParser(); | ||
const parser = new ListParser(stringParser, { separators: ['-', '|'] }); | ||
const response = parser.parseValue('boop | bop | beep'); | ||
expect(response).to.deep.equal(['boop', 'bop', 'beep']); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
150420
1473