diff --git a/config.test.mjs b/config.test.mjs index d123232..2678f8e 100644 --- a/config.test.mjs +++ b/config.test.mjs @@ -2,6 +2,6 @@ import { test } from 'node:test' import * as assert from 'node:assert' import { getConfigPath } from './config.mjs' -test('test getConfigPath()', async () => { +test('getConfigPath()', async () => { assert.ok((await getConfigPath(false)).endsWith('/.npmrc')) }) diff --git a/nrmrc.mjs b/nrmrc.mjs index 977a6e7..76535a3 100644 --- a/nrmrc.mjs +++ b/nrmrc.mjs @@ -47,18 +47,20 @@ export async function readNrmrc() { /** * @param {string} name */ -function encodeName(name) { - const s = JSON.stringify(name).slice(1, -1) - if (name === s) return name - return `"${s}"` +export function encodeName(name) { + if (name.length > 1 && name.startsWith('"') && name.endsWith('"')) { + return `"${name.replaceAll('"', String.raw`\"`)}"` + } else { + return name + } } /** * @param {string} name */ -function decodeName(name) { - if (name.startsWith('"') && name.endsWith('"')) { - return JSON.parse(name) +export function decodeName(name) { + if (name.length > 1 && name.startsWith('"') && name.endsWith('"')) { + return name.slice(1, -1).replaceAll(String.raw`\"`, '"') } else { return name } diff --git a/nrmrc.test.mjs b/nrmrc.test.mjs new file mode 100644 index 0000000..e394391 --- /dev/null +++ b/nrmrc.test.mjs @@ -0,0 +1,20 @@ +import { test } from 'node:test' +import * as assert from 'node:assert' + +import { encodeName, decodeName } from './nrmrc.mjs' + +test('encodeName()', () => { + assert.strictEqual(encodeName('name'), 'name') + assert.strictEqual(encodeName('"'), '"') + assert.strictEqual(encodeName(`"[`), String.raw`"[`) + assert.strictEqual(encodeName(`""`), String.raw`"\"\""`) +}) + +test('decodeName()', () => { + assert.strictEqual(decodeName('name'), 'name') + assert.strictEqual(decodeName('"'), '"') + assert.strictEqual(decodeName(String.raw`"[`), `"[`) + assert.strictEqual(decodeName(String.raw`"["`), '[') + assert.strictEqual(decodeName(String.raw`"\"["`), '"[') + assert.strictEqual(decodeName(String.raw`"\"\""`), '""') +}) diff --git a/registry.test.mjs b/registry.test.mjs index a6dc345..9bc4ccf 100644 --- a/registry.test.mjs +++ b/registry.test.mjs @@ -3,7 +3,7 @@ import * as assert from 'node:assert' import { Readable } from 'node:stream' import { getRegistryFromStream } from './registry.mjs' -test('test getRegistryFromStream()', async () => { +test('getRegistryFromStream()', async () => { assert.deepStrictEqual( await getRegistryFromStream(Readable.from('')), undefined