Better string sanitization

This commit is contained in:
Piotr Rogowski 2021-09-27 20:41:25 +02:00
parent f0cffb2d04
commit d142f37114
No known key found for this signature in database
GPG Key ID: F40F61D5587F5673
8 changed files with 212 additions and 207 deletions

View File

@ -1135,9 +1135,14 @@ export class INI {
.tryParse(line);
}
private static numberOrExpression = (val: string | undefined | null) => INI.isNumber(val || '0') ? Number(val || 0) : INI.sanitize(`${val}`);
private static numberOrExpression = (val: string | undefined | null) =>
INI.isNumber(val || '0') ? Number(val || 0) : INI.sanitize(`${val}`);
private static sanitize = (val: any) => val === undefined ? '' : `${val}`.replace(/"/g, '').trim();
private static sanitize = (val: any) =>
val === undefined ? '' : `${val}`
.replace(/"/g, '')
.replace(/\s+/g, ' ')
.trim();
private static isNumber = (val: any) => !Number.isNaN(Number(val));

View File

@ -47,9 +47,9 @@ VERSIONS.forEach((version) => {
fs.writeFileSync(pathFor(`tmp/${version}.yml`), yamlContent);
fs.writeFileSync(pathFor(`tmp/${version}.json`), jsonContent);
assert.equal(yamlNew, yamlOld);
assert.equal(jsonNew, jsonOld);
assert.equal(yamlNew, yamlOld, `Generated file ${version}.yaml looks different than expected`);
assert.equal(jsonNew, jsonOld, `Generated file ${version}.json looks different than expected`);
fs.unlinkSync(pathFor(`tmp/${version}.yml`));
fs.unlinkSync(pathFor(`tmp/${version}.json`));
// fs.unlinkSync(pathFor(`tmp/${version}.yml`));
// fs.unlinkSync(pathFor(`tmp/${version}.json`));
});