Skip to content

Commit

Permalink
fix: fix nrmrc parser
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Aug 11, 2024
1 parent 69f4266 commit 056b4b7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
16 changes: 9 additions & 7 deletions nrmrc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions nrmrc.test.mjs
Original file line number Diff line number Diff line change
@@ -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`"\"\""`), '""')
})
2 changes: 1 addition & 1 deletion registry.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 056b4b7

Please sign in to comment.