From d3671072416303844166028c71293505c5bf76f0 Mon Sep 17 00:00:00 2001 From: Casey Occhialini <1508707+littlespex@users.noreply.github.com> Date: Thu, 18 Jan 2024 15:10:53 -0800 Subject: [PATCH] fix: clean up imports (#65) see: #64 --------- Signed-off-by: Casey Occhialini <1508707+littlespex@users.noreply.github.com> --- CHANGELOG.md | 5 +++- lib/config/common-media-library.api.md | 6 ++--- lib/src/cmcd.ts | 2 ++ lib/src/cmcd/CmcdFormatters.ts | 2 +- lib/src/cmcd/decodeCmcd.ts | 2 +- lib/src/cmcd/encodeCmcd.ts | 2 +- lib/src/cmcd/toCmcdJson.ts | 2 +- lib/src/cmsd/CmsdDynamic.ts | 2 +- lib/src/cmsd/CmsdDynamicParams.ts | 4 +-- lib/src/cta/utils/isValid.ts | 2 +- lib/src/cta/utils/symbolToStr.ts | 2 +- lib/src/id3/isId3TimestampFrame.ts | 2 +- lib/src/id3/util/isId3Header.ts | 1 - lib/src/request/CommonMediaRequest.ts | 2 +- lib/src/structuredfield/SfDecodeOptions.ts | 1 - lib/src/structuredfield/SfEncodeOptions.ts | 1 - lib/src/structuredfield/decodeSfItem.ts | 6 +++-- lib/src/structuredfield/parse/ParsedValue.ts | 4 +++ lib/src/structuredfield/parse/parseBoolean.ts | 16 +++++------ .../parse/parseByteSequence.ts | 11 +++++--- lib/src/structuredfield/parse/parseDate.ts | 13 +++++---- lib/src/structuredfield/parse/parseDict.ts | 25 +++++++++-------- .../structuredfield/parse/parseInnerList.ts | 15 +++++++---- .../parse/parseIntegerOrDecimal.ts | 25 +++++++++-------- lib/src/structuredfield/parse/parseItem.ts | 9 ++++--- .../parse/parseItemOrInnerList.ts | 1 + lib/src/structuredfield/parse/parseKey.ts | 16 +++++------ lib/src/structuredfield/parse/parseList.ts | 10 +++---- .../structuredfield/parse/parseParameters.ts | 11 ++++---- lib/src/structuredfield/parse/parseString.ts | 27 +++++++++++-------- lib/src/structuredfield/parse/parseToken.ts | 10 ++++--- lib/src/structuredfield/utils/ITEM.ts | 1 + 32 files changed, 131 insertions(+), 107 deletions(-) create mode 100644 lib/src/structuredfield/utils/ITEM.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e2550ff6..8e9b3b1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [0.6.2] - ????-??-?? +## [0.6.2] - 2023-01-18 + +### Fixed +- Some imports are pulling in unnecessary files [#64](https://github.com/streaming-video-technology-alliance/common-media-library/issues/64) ## [0.6.1] - 2023-12-14 diff --git a/lib/config/common-media-library.api.md b/lib/config/common-media-library.api.md index 7ec13c49..1d0a1550 100644 --- a/lib/config/common-media-library.api.md +++ b/lib/config/common-media-library.api.md @@ -120,10 +120,10 @@ export const CMSD_V1 = 1; export type CmsdCustomKey = CmCustomKey; // @beta -export type CmsdDynamic = { - value: string; +export interface CmsdDynamic { params: CmsdDynamicParams; -}; + value: string; +} // @beta export interface CmsdDynamicParams { diff --git a/lib/src/cmcd.ts b/lib/src/cmcd.ts index 37c5c939..e0c0a715 100644 --- a/lib/src/cmcd.ts +++ b/lib/src/cmcd.ts @@ -2,6 +2,8 @@ * A collection of tools for working with Common Media Client Data (CMCD). * * @packageDocumentation + * + * @beta */ export { CMCD_PARAM } from './cmcd/CMCD_PARAM.js'; export { CMCD_V1 } from './cmcd/CMCD_V1.js'; diff --git a/lib/src/cmcd/CmcdFormatters.ts b/lib/src/cmcd/CmcdFormatters.ts index 2a550da2..8e83ae83 100644 --- a/lib/src/cmcd/CmcdFormatters.ts +++ b/lib/src/cmcd/CmcdFormatters.ts @@ -1,4 +1,4 @@ -import { urlToRelativePath } from '../utils.js'; +import { urlToRelativePath } from '../utils/urlToRelativePath.js'; import { CmcdEncodeOptions } from './CmcdEncodeOptions.js'; import { CmcdFormatter } from './CmcdFormatter.js'; import { CmcdValue } from './CmcdValue.js'; diff --git a/lib/src/cmcd/decodeCmcd.ts b/lib/src/cmcd/decodeCmcd.ts index 19e9962b..19704e76 100644 --- a/lib/src/cmcd/decodeCmcd.ts +++ b/lib/src/cmcd/decodeCmcd.ts @@ -1,5 +1,5 @@ import { symbolToStr } from '../cta/utils/symbolToStr.js'; -import { SfItem } from '../structuredfield.js'; +import { SfItem } from '../structuredfield/SfItem.js'; import { decodeSfDict } from '../structuredfield/decodeSfDict.js'; import { Cmcd } from './Cmcd.js'; diff --git a/lib/src/cmcd/encodeCmcd.ts b/lib/src/cmcd/encodeCmcd.ts index 78dc7b3d..188a45c1 100644 --- a/lib/src/cmcd/encodeCmcd.ts +++ b/lib/src/cmcd/encodeCmcd.ts @@ -1,4 +1,4 @@ -import { encodeSfDict } from '../structuredfield.js'; +import { encodeSfDict } from '../structuredfield/encodeSfDict.js'; import { Cmcd } from './Cmcd.js'; import { CmcdEncodeOptions } from './CmcdEncodeOptions.js'; import { processCmcd } from './utils/processCmcd.js'; diff --git a/lib/src/cmcd/toCmcdJson.ts b/lib/src/cmcd/toCmcdJson.ts index 149c1847..8f6ad7a8 100644 --- a/lib/src/cmcd/toCmcdJson.ts +++ b/lib/src/cmcd/toCmcdJson.ts @@ -1,5 +1,5 @@ import { symbolToStr } from '../cta/utils/symbolToStr.js'; -import { SfToken } from '../structuredfield.js'; +import { SfToken } from '../structuredfield/SfToken.js'; import { Cmcd } from './Cmcd.js'; import { CmcdEncodeOptions } from './CmcdEncodeOptions.js'; import { processCmcd } from './utils/processCmcd.js'; diff --git a/lib/src/cmsd/CmsdDynamic.ts b/lib/src/cmsd/CmsdDynamic.ts index 1530d8b6..6f7c80d6 100644 --- a/lib/src/cmsd/CmsdDynamic.ts +++ b/lib/src/cmsd/CmsdDynamic.ts @@ -1,4 +1,4 @@ -import { CmsdDynamicParams } from './CmsdDynamicParams'; +import { CmsdDynamicParams } from './CmsdDynamicParams.js'; /** * Common Media Server Data (CMSD) dynamic response header fields. diff --git a/lib/src/cmsd/CmsdDynamicParams.ts b/lib/src/cmsd/CmsdDynamicParams.ts index cc498009..055669b0 100644 --- a/lib/src/cmsd/CmsdDynamicParams.ts +++ b/lib/src/cmsd/CmsdDynamicParams.ts @@ -1,5 +1,5 @@ -import { CmsdCustomKey } from './CmsdCustomKey'; -import { CmsdValue } from './CmsdValue'; +import { CmsdCustomKey } from './CmsdCustomKey.js'; +import { CmsdValue } from './CmsdValue.js'; /** * Common Media Server Data (CMSD) dynamic response header field parameters. diff --git a/lib/src/cta/utils/isValid.ts b/lib/src/cta/utils/isValid.ts index 10fb12c4..eb536317 100644 --- a/lib/src/cta/utils/isValid.ts +++ b/lib/src/cta/utils/isValid.ts @@ -1,4 +1,4 @@ -import { CmValue } from '../CmValue'; +import { CmValue } from '../CmValue.js'; export const isValid = (value: CmValue) => { if (typeof value === 'number') { diff --git a/lib/src/cta/utils/symbolToStr.ts b/lib/src/cta/utils/symbolToStr.ts index bb0a715f..1a0c669b 100644 --- a/lib/src/cta/utils/symbolToStr.ts +++ b/lib/src/cta/utils/symbolToStr.ts @@ -1,4 +1,4 @@ -import { SfToken } from '../../structuredfield/SfToken'; +import { SfToken } from '../../structuredfield/SfToken.js'; export function symbolToStr(symbol: symbol | SfToken) { return symbol.description || symbol.toString().slice(7, -1); diff --git a/lib/src/id3/isId3TimestampFrame.ts b/lib/src/id3/isId3TimestampFrame.ts index 6602f086..1967df2e 100644 --- a/lib/src/id3/isId3TimestampFrame.ts +++ b/lib/src/id3/isId3TimestampFrame.ts @@ -1,4 +1,4 @@ -import { Id3Frame } from './Id3Frame'; +import { Id3Frame } from './Id3Frame.js'; /** * Returns true if the ID3 frame is an Elementary Stream timestamp frame diff --git a/lib/src/id3/util/isId3Header.ts b/lib/src/id3/util/isId3Header.ts index 728dc0d0..a2066517 100644 --- a/lib/src/id3/util/isId3Header.ts +++ b/lib/src/id3/util/isId3Header.ts @@ -1,4 +1,3 @@ - /** * Returns true if an ID3 header can be found at offset in data * diff --git a/lib/src/request/CommonMediaRequest.ts b/lib/src/request/CommonMediaRequest.ts index d80871fc..2aa932eb 100644 --- a/lib/src/request/CommonMediaRequest.ts +++ b/lib/src/request/CommonMediaRequest.ts @@ -1,4 +1,4 @@ -import { Cmcd } from '../cmcd.js'; +import { Cmcd } from '../cmcd/Cmcd.js'; /** * Common request API. diff --git a/lib/src/structuredfield/SfDecodeOptions.ts b/lib/src/structuredfield/SfDecodeOptions.ts index d613ded5..1e3ed116 100644 --- a/lib/src/structuredfield/SfDecodeOptions.ts +++ b/lib/src/structuredfield/SfDecodeOptions.ts @@ -1,4 +1,3 @@ - /** * Options for decoding Structured Field values * diff --git a/lib/src/structuredfield/SfEncodeOptions.ts b/lib/src/structuredfield/SfEncodeOptions.ts index c17e49c0..4de30cec 100644 --- a/lib/src/structuredfield/SfEncodeOptions.ts +++ b/lib/src/structuredfield/SfEncodeOptions.ts @@ -1,4 +1,3 @@ - /** * Options for encoding a structured field. * diff --git a/lib/src/structuredfield/decodeSfItem.ts b/lib/src/structuredfield/decodeSfItem.ts index 5aa4d067..1f0335ea 100644 --- a/lib/src/structuredfield/decodeSfItem.ts +++ b/lib/src/structuredfield/decodeSfItem.ts @@ -1,6 +1,7 @@ import { SfDecodeOptions } from './SfDecodeOptions.js'; import { parseError } from './parse/parseError.js'; import { parseItem } from './parse/parseItem.js'; +import { ITEM } from './utils/ITEM.js'; // 4.2. Parsing Structured Fields // @@ -37,11 +38,12 @@ export function decodeSfItem(input: string, options?: SfDecodeOptions) { try { const { src, value } = parseItem(input.trim(), options); if (src !== '') { - throw parseError(src, 'Item'); + throw parseError(src, ITEM); } + return value; } catch (cause) { - throw parseError(input, 'Item', cause); + throw parseError(input, ITEM, cause); } } diff --git a/lib/src/structuredfield/parse/ParsedValue.ts b/lib/src/structuredfield/parse/ParsedValue.ts index 6e97c06f..a2d5296c 100644 --- a/lib/src/structuredfield/parse/ParsedValue.ts +++ b/lib/src/structuredfield/parse/ParsedValue.ts @@ -2,3 +2,7 @@ export type ParsedValue = { value: T; src: string; } + +export function parsedValue(value: T, src: string): ParsedValue { + return { value, src }; +} diff --git a/lib/src/structuredfield/parse/parseBoolean.ts b/lib/src/structuredfield/parse/parseBoolean.ts index 71a7a1eb..6c47d61b 100644 --- a/lib/src/structuredfield/parse/parseBoolean.ts +++ b/lib/src/structuredfield/parse/parseBoolean.ts @@ -1,5 +1,5 @@ import { BOOLEAN } from '../utils/BOOLEAN.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; // 4.2.8. Parsing a Boolean @@ -23,18 +23,16 @@ export function parseBoolean(src: string): ParsedValue { if (src[i] !== '?') { throw parseError(src, BOOLEAN); } + i++; + if (src[i] === '1') { - return { - value: true, - src: src.substring(++i), - }; + return parsedValue(true, src.substring(++i)); } + if (src[i] === '0') { - return { - value: false, - src: src.substring(++i), - }; + return parsedValue(false, src.substring(++i)); } + throw parseError(src, BOOLEAN); } diff --git a/lib/src/structuredfield/parse/parseByteSequence.ts b/lib/src/structuredfield/parse/parseByteSequence.ts index 0b7ed388..7bfaceac 100644 --- a/lib/src/structuredfield/parse/parseByteSequence.ts +++ b/lib/src/structuredfield/parse/parseByteSequence.ts @@ -1,6 +1,6 @@ import { base64decode } from '../../utils/base64decode.js'; import { BYTES } from '../utils/BYTES.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; // 4.2.7. Parsing a Byte Sequence @@ -47,16 +47,19 @@ export function parseByteSequence(src: string): ParsedValue { if (src[0] !== ':') { throw parseError(src, BYTES); } + src = src.substring(1); if (src.includes(':') === false) { throw parseError(src, BYTES); } + const re = /(^.*?)(:)/g; const b64_content = (re.exec(src) as any)[1]; src = src.substring(re.lastIndex); + // pass b64_content char check step 6 - return { - value: base64decode(b64_content), + return parsedValue( + base64decode(b64_content), src, - }; + ); } diff --git a/lib/src/structuredfield/parse/parseDate.ts b/lib/src/structuredfield/parse/parseDate.ts index a5119b4b..8ece7e7b 100644 --- a/lib/src/structuredfield/parse/parseDate.ts +++ b/lib/src/structuredfield/parse/parseDate.ts @@ -1,5 +1,5 @@ import { DATE } from '../utils/DATE.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; import { parseIntegerOrDecimal } from './parseIntegerOrDecimal.js'; @@ -23,13 +23,16 @@ export function parseDate(src: string): ParsedValue { if (src[i] !== '@') { throw parseError(src, DATE); } + i++; const date = parseIntegerOrDecimal(src.substring(i)); + if (Number.isInteger(date.value) === false) { throw parseError(src, DATE); } - return { - value: new Date(date.value * 1000), - src: date.src, - }; + + return parsedValue( + new Date(date.value * 1000), + date.src, + ); } diff --git a/lib/src/structuredfield/parse/parseDict.ts b/lib/src/structuredfield/parse/parseDict.ts index 4d956ca0..dc6e31fd 100644 --- a/lib/src/structuredfield/parse/parseDict.ts +++ b/lib/src/structuredfield/parse/parseDict.ts @@ -3,7 +3,7 @@ import { SfDictionary } from '../SfDictionary.js'; import { SfInnerList } from '../SfInnerList.js'; import { SfItem } from '../SfItem.js'; import { DICT } from '../utils/DICT.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; import { parseItemOrInnerList } from './parseItemOrInnerList.js'; import { parseKey } from './parseKey.js'; @@ -60,17 +60,14 @@ import { parseParameters } from './parseParameters.js'; // Note that when duplicate Dictionary keys are encountered, this has // the effect of ignoring all but the last instance. export function parseDict(src: string, options?: SfDecodeOptions): ParsedValue { - const value: [string, SfItem | SfInnerList][] = []; - - function toDict(entries: [string, SfItem | SfInnerList][]) { - return Object.fromEntries(entries); - } + const value: SfDictionary = {}; while (src.length > 0) { let member: SfItem | SfInnerList; const parsedKey = parseKey(src); - const this_key = parsedKey.value; + const key = parsedKey.value; src = parsedKey.src; + if (src[0] === '=') { const parsedItemOrInnerList = parseItemOrInnerList(src.substring(1), options); member = parsedItemOrInnerList.value; @@ -81,21 +78,23 @@ export function parseDict(src: string, options?: SfDecodeOptions): ParsedValue 0) { @@ -51,17 +52,21 @@ export function parseInnerList(src: string, options?: SfDecodeOptions): ParsedVa if (src[0] === ')') { src = src.substring(1); const parsedParameters = parseParameters(src, options); - return { - value: new SfItem(innerList, parsedParameters.value) as any, - src: parsedParameters.src, - }; + + return parsedValue( + new SfItem(innerList, parsedParameters.value) as any, + parsedParameters.src, + ); } + const parsedItem = parseItem(src, options); innerList.push(parsedItem.value); src = parsedItem.src; + if (src[0] !== ' ' && src[0] !== ')') { throw parseError(src, INNER); } } + throw parseError(src, INNER); } diff --git a/lib/src/structuredfield/parse/parseIntegerOrDecimal.ts b/lib/src/structuredfield/parse/parseIntegerOrDecimal.ts index 2c5546f8..cabea95f 100644 --- a/lib/src/structuredfield/parse/parseIntegerOrDecimal.ts +++ b/lib/src/structuredfield/parse/parseIntegerOrDecimal.ts @@ -1,6 +1,6 @@ import { INTEGER_DECIMAL } from '../utils/INTEGER_DECIMAL.js'; import { isInvalidInt } from '../utils/isInvalidInt.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; // 4.2.4. Parsing an Integer or Decimal @@ -74,6 +74,7 @@ export function parseIntegerOrDecimal(src: string): ParsedValue { let num = ''; let value; const i = 0; + const error = parseError(orig, INTEGER_DECIMAL); if (src[i] === '-') { sign = -1; @@ -81,13 +82,13 @@ export function parseIntegerOrDecimal(src: string): ParsedValue { } if (src.length <= 0) { - throw parseError(orig, 'Integer or Decimal'); + throw error; } const re_integer = /^(\d+)?/g; const result_integer = re_integer.exec(src) as any; if (result_integer[0].length === 0) { - throw parseError(orig, 'Integer or Decimal'); + throw error; } num += result_integer[1]; src = src.substring(re_integer.lastIndex); @@ -95,35 +96,37 @@ export function parseIntegerOrDecimal(src: string): ParsedValue { if (src[0] === '.') { // decimal if (num.length > 12) { - throw parseError(orig, 'Integer or Decimal'); + throw error; } + const re_decimal = /^(\.\d+)?/g; const result_decimal = re_decimal.exec(src) as any; src = src.substring(re_decimal.lastIndex); // 9.2. If the number of characters after "." in input_number is greater than three, fail parsing. if (result_decimal[0].length === 0 || result_decimal[1].length > 4) { - throw parseError(orig, INTEGER_DECIMAL); + throw error; } + num += result_decimal[1]; // 7.6. If type is "decimal" and input_number contains more than 16 characters, fail parsing. if (num.length > 16) { - throw parseError(orig, INTEGER_DECIMAL); + throw error; } + value = parseFloat(num) * sign; } else { // integer // 7.5. If type is "integer" and input_number contains more than 15 characters, fail parsing. if (num.length > 15) { - throw parseError(orig, INTEGER_DECIMAL); + throw error; } + value = parseInt(num) * sign; if (isInvalidInt(value)) { throw parseError(num, INTEGER_DECIMAL); } } - return { - value, - src, - }; + + return parsedValue(value, src); } diff --git a/lib/src/structuredfield/parse/parseItem.ts b/lib/src/structuredfield/parse/parseItem.ts index 271deba3..35b0e9f2 100644 --- a/lib/src/structuredfield/parse/parseItem.ts +++ b/lib/src/structuredfield/parse/parseItem.ts @@ -1,6 +1,6 @@ import { SfDecodeOptions } from '../SfDecodeOptions.js'; import { SfItem } from '../SfItem.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseBareItem } from './parseBareItem.js'; import { parseParameters } from './parseParameters.js'; @@ -20,11 +20,12 @@ import { parseParameters } from './parseParameters.js'; export function parseItem(src: string, options?: SfDecodeOptions): ParsedValue { const parsedBareItem = parseBareItem(src, options); src = parsedBareItem.src; + const parsedParameters = parseParameters(src, options); src = parsedParameters.src; - return { - value: new SfItem(parsedBareItem.value, parsedParameters.value), + return parsedValue( + new SfItem(parsedBareItem.value, parsedParameters.value), src, - }; + ); } diff --git a/lib/src/structuredfield/parse/parseItemOrInnerList.ts b/lib/src/structuredfield/parse/parseItemOrInnerList.ts index c801dbb3..27361c99 100644 --- a/lib/src/structuredfield/parse/parseItemOrInnerList.ts +++ b/lib/src/structuredfield/parse/parseItemOrInnerList.ts @@ -22,5 +22,6 @@ export function parseItemOrInnerList(src: string, options?: SfDecodeOptions): Pa if (src[0] === '(') { return parseInnerList(src, options); } + return parseItem(src, options); } diff --git a/lib/src/structuredfield/parse/parseKey.ts b/lib/src/structuredfield/parse/parseKey.ts index ca3b8712..232a4007 100644 --- a/lib/src/structuredfield/parse/parseKey.ts +++ b/lib/src/structuredfield/parse/parseKey.ts @@ -1,5 +1,5 @@ import { KEY } from '../utils/KEY.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; // 4.2.3.3. Parsing a Key @@ -28,19 +28,17 @@ export function parseKey(src: string): ParsedValue { if (/^[a-z*]$/.test(src[i]) === false) { throw parseError(src, KEY); } + let value = ''; + while (src.length > i) { if (/^[a-z0-9_\-.*]$/.test(src[i]) === false) { - return { - value, - src: src.substring(i), - }; + return parsedValue(value, src.substring(i)); } + value += src[i]; i++; } - return { - value, - src: src.substring(i), - }; + + return parsedValue(value, src.substring(i)); } diff --git a/lib/src/structuredfield/parse/parseList.ts b/lib/src/structuredfield/parse/parseList.ts index f8fc532d..a5f80b60 100644 --- a/lib/src/structuredfield/parse/parseList.ts +++ b/lib/src/structuredfield/parse/parseList.ts @@ -1,7 +1,7 @@ import { SfDecodeOptions } from '../SfDecodeOptions.js'; import { SfMember } from '../SfMember.js'; import { LIST } from '../utils/LIST.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; import { parseItemOrInnerList } from './parseItemOrInnerList.js'; @@ -40,7 +40,7 @@ export function parseList(src: string, options?: SfDecodeOptions): ParsedValue { let output = ''; let i = 0; + if (src[i] !== `"`) { - throw parseError(src, 'String'); + throw parseError(src, STRING); } + i++; + while (src.length > i) { - // console.log(i, input_string[i], output_string) if (src[i] === `\\`) { if (src.length <= i + 1) { - throw parseError(src, 'String'); + throw parseError(src, STRING); } + i++; + if (src[i] !== `"` && src[i] !== `\\`) { - throw parseError(src, 'String'); + throw parseError(src, STRING); } + output += src[i]; } else if (src[i] === `"`) { - return { - value: output, - src: src.substring(++i), - }; + return parsedValue(output, src.substring(++i)); } else if (STRING_REGEX.test(src[i])) { - throw parseError(src, 'String'); + throw parseError(src, STRING); } else { output += src[i]; } + i++; } - throw parseError(src, 'String'); + + throw parseError(src, STRING); } diff --git a/lib/src/structuredfield/parse/parseToken.ts b/lib/src/structuredfield/parse/parseToken.ts index 87166c07..dc521f16 100644 --- a/lib/src/structuredfield/parse/parseToken.ts +++ b/lib/src/structuredfield/parse/parseToken.ts @@ -1,7 +1,7 @@ import { SfDecodeOptions } from '../SfDecodeOptions.js'; import { SfToken } from '../SfToken.js'; import { TOKEN } from '../utils/TOKEN.js'; -import { ParsedValue } from './ParsedValue.js'; +import { ParsedValue, parsedValue } from './ParsedValue.js'; import { parseError } from './parseError.js'; // 4.2.6. Parsing a Token @@ -29,11 +29,13 @@ export function parseToken(src: string, options?: SfDecodeOptions): ParsedValue< if (/^[a-zA-Z*]$/.test(src[0]) === false) { throw parseError(src, TOKEN); } + const re = /^([!#$%&'*+\-.^_`|~\w:/]+)/g; const value = (re.exec(src) as any)[1]; src = src.substring(re.lastIndex); - return { - value: options?.useSymbol === false ? new SfToken(value) : Symbol.for(value), + + return parsedValue( + options?.useSymbol === false ? new SfToken(value) : Symbol.for(value), src, - }; + ); } diff --git a/lib/src/structuredfield/utils/ITEM.ts b/lib/src/structuredfield/utils/ITEM.ts new file mode 100644 index 00000000..69d5d7b3 --- /dev/null +++ b/lib/src/structuredfield/utils/ITEM.ts @@ -0,0 +1 @@ +export const ITEM = 'Item';