-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: slight reorganization * feat: rough draft of a serializer * fix: make stringify spec-compliant * chore: code cleanup and benchmarks * chore: update readme * fix: reject null/undefined in arrays * chore: update readme * fix: reject invalid dates
- Loading branch information
Showing
16 changed files
with
1,293 additions
and
632 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,49 @@ | ||
/*! | ||
* Copyright (c) Squirrel Chat et al., All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* 3. Neither the name of the copyright holder nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
import { bench } from 'vitest' | ||
import { readFile } from 'fs/promises' | ||
import { stringify as smolTomlStringify, parse } from '../src/index.js' | ||
import { stringify as iarnaTomlStringify } from '@iarna/toml' | ||
import { stringify as ltdJTomlStringify } from '@ltd/j-toml' | ||
|
||
let obj = parse( | ||
await readFile(new URL('./testfiles/5mb-mixed.toml', import.meta.url), 'utf8') | ||
) | ||
|
||
bench('smol-toml', () => { | ||
smolTomlStringify(obj) | ||
}) | ||
|
||
bench('@iarna/toml', () => { | ||
iarnaTomlStringify(obj) | ||
}) | ||
|
||
bench('@ltd/j-toml', () => { | ||
ltdJTomlStringify(obj) | ||
}) |
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,49 @@ | ||
/*! | ||
* Copyright (c) Squirrel Chat et al., All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* 3. Neither the name of the copyright holder nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
import { bench } from 'vitest' | ||
import { readFile } from 'fs/promises' | ||
import { stringify as smolTomlStringify, parse } from '../src/index.js' | ||
import { stringify as iarnaTomlStringify } from '@iarna/toml' | ||
import { stringify as ltdJTomlStringify } from '@ltd/j-toml' | ||
|
||
let obj = parse( | ||
await readFile(new URL('./testfiles/toml-spec-example.toml', import.meta.url), 'utf8') | ||
) | ||
|
||
bench('smol-toml', () => { | ||
smolTomlStringify(obj) | ||
}) | ||
|
||
bench('@iarna/toml', () => { | ||
iarnaTomlStringify(obj) | ||
}) | ||
|
||
bench('@ltd/j-toml', () => { | ||
ltdJTomlStringify(obj) | ||
}) |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
{ | ||
"name": "smol-toml", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"keywords": [ | ||
"toml", | ||
"parser" | ||
"parser", | ||
"serializer" | ||
], | ||
"description": "A small, fast, and correct TOML parser", | ||
"description": "A small, fast, and correct TOML parser/serializer", | ||
"repository": "[email protected]:squirrelchat/smol-toml.git", | ||
"author": "Cynthia <[email protected]>", | ||
"license": "BSD-3-Clause", | ||
|
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,105 @@ | ||
/*! | ||
* Copyright (c) Squirrel Chat et al., All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* 3. Neither the name of the copyright holder nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
import { parseString, parseValue } from './primitive.js' | ||
import { parseArray, parseInlineTable } from './struct.js' | ||
import { type TomlPrimitive, indexOfNewline, skipVoid, skipUntil, skipComment, getStringEnd } from './util.js' | ||
import TomlError from './error.js' | ||
|
||
function sliceAndTrimEndOf (str: string, startPtr: number, endPtr: number, allowNewLines?: boolean): [ string, number ] { | ||
let value = str.slice(startPtr, endPtr) | ||
|
||
let commentIdx = value.indexOf('#') | ||
if (commentIdx > -1) { | ||
// The call to skipComment allows to "validate" the comment | ||
// (absence of control characters) | ||
skipComment(str, commentIdx) | ||
value = value.slice(0, commentIdx) | ||
} | ||
|
||
let trimmed = value.trimEnd() | ||
|
||
if (!allowNewLines) { | ||
let newlineIdx = value.indexOf('\n', trimmed.length) | ||
if (newlineIdx > -1) { | ||
throw new TomlError('newlines are not allowed in inline tables', { | ||
toml: str, | ||
ptr: startPtr + newlineIdx | ||
}) | ||
} | ||
} | ||
|
||
return [ trimmed, commentIdx ] | ||
} | ||
|
||
export function extractValue (str: string, ptr: number, end?: string): [ TomlPrimitive, number ] { | ||
let c = str[ptr] | ||
if (c === '[' || c === '{') { | ||
let [ value, endPtr ] = c === '[' | ||
? parseArray(str, ptr) | ||
: parseInlineTable(str, ptr) | ||
|
||
let newPtr = skipUntil(str, endPtr, ',', end) | ||
if (end === '}') { | ||
let nextNewLine = indexOfNewline(str, endPtr, newPtr) | ||
if (nextNewLine > -1) { | ||
throw new TomlError('newlines are not allowed in inline tables', { | ||
toml: str, | ||
ptr: nextNewLine | ||
}) | ||
} | ||
} | ||
|
||
return [ value, newPtr ] | ||
} | ||
|
||
let endPtr | ||
if (c === '"' || c === "'") { | ||
endPtr = getStringEnd(str, ptr) | ||
return [ parseString(str, ptr, endPtr), endPtr + +(!!end && str[endPtr] === ',') ] | ||
} | ||
|
||
endPtr = skipUntil(str, ptr, ',', end) | ||
let slice = sliceAndTrimEndOf(str, ptr, endPtr - (+(str[endPtr - 1] === ',')), end === ']') | ||
if (!slice[0]) { | ||
throw new TomlError('incomplete key-value declaration: no value specified', { | ||
toml: str, | ||
ptr: ptr | ||
}) | ||
} | ||
|
||
if (end && slice[1] > -1) { | ||
endPtr = skipVoid(str, ptr + slice[1]) | ||
endPtr += +(str[endPtr] === ',') | ||
} | ||
|
||
return [ | ||
parseValue(slice[0], str, ptr), | ||
endPtr, | ||
] | ||
} |
Oops, something went wrong.