nip54 normalizeIdentifier function
This commit is contained in:
parent
e7e8db1dbd
commit
3c4019a154
1
index.ts
1
index.ts
|
@ -21,6 +21,7 @@ export * as nip39 from './nip39.ts'
|
||||||
export * as nip42 from './nip42.ts'
|
export * as nip42 from './nip42.ts'
|
||||||
export * as nip44 from './nip44.ts'
|
export * as nip44 from './nip44.ts'
|
||||||
export * as nip47 from './nip47.ts'
|
export * as nip47 from './nip47.ts'
|
||||||
|
export * as nip54 from './nip54.ts'
|
||||||
export * as nip57 from './nip57.ts'
|
export * as nip57 from './nip57.ts'
|
||||||
export * as nip59 from './nip59.ts'
|
export * as nip59 from './nip59.ts'
|
||||||
export * as nip98 from './nip98.ts'
|
export * as nip98 from './nip98.ts'
|
||||||
|
|
1
jsr.json
1
jsr.json
|
@ -34,6 +34,7 @@
|
||||||
"./nip44": "./nip44.ts",
|
"./nip44": "./nip44.ts",
|
||||||
"./nip46": "./nip46.ts",
|
"./nip46": "./nip46.ts",
|
||||||
"./nip49": "./nip49.ts",
|
"./nip49": "./nip49.ts",
|
||||||
|
"./nip54": "./nip54.ts",
|
||||||
"./nip57": "./nip57.ts",
|
"./nip57": "./nip57.ts",
|
||||||
"./nip58": "./nip58.ts",
|
"./nip58": "./nip58.ts",
|
||||||
"./nip59": "./nip59.ts",
|
"./nip59": "./nip59.ts",
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
import { describe, test, expect } from 'bun:test'
|
||||||
|
import { normalizeIdentifier } from './nip54.ts'
|
||||||
|
|
||||||
|
describe('normalizeIdentifier', () => {
|
||||||
|
test('converts to lowercase', () => {
|
||||||
|
expect(normalizeIdentifier('HELLO')).toBe('hello')
|
||||||
|
expect(normalizeIdentifier('MixedCase')).toBe('mixedcase')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('trims whitespace', () => {
|
||||||
|
expect(normalizeIdentifier(' hello ')).toBe('hello')
|
||||||
|
expect(normalizeIdentifier('\thello\n')).toBe('hello')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('normalizes Unicode to NFKC form', () => {
|
||||||
|
// é can be represented as single char é (U+00E9) or e + ´ (U+0065 U+0301)
|
||||||
|
expect(normalizeIdentifier('café')).toBe('café')
|
||||||
|
expect(normalizeIdentifier('cafe\u0301')).toBe('café')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('replaces non-alphanumeric characters with hyphens', () => {
|
||||||
|
expect(normalizeIdentifier('hello world')).toBe('hello-world')
|
||||||
|
expect(normalizeIdentifier('user@example.com')).toBe('user-example-com')
|
||||||
|
expect(normalizeIdentifier('$special#chars!')).toBe('-special-chars-')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('preserves numbers', () => {
|
||||||
|
expect(normalizeIdentifier('user123')).toBe('user123')
|
||||||
|
expect(normalizeIdentifier('2fast4you')).toBe('2fast4you')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('handles multiple consecutive special characters', () => {
|
||||||
|
expect(normalizeIdentifier('hello!!!world')).toBe('hello---world')
|
||||||
|
expect(normalizeIdentifier('multiple spaces')).toBe('multiple---spaces')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('handles Unicode letters from different scripts', () => {
|
||||||
|
expect(normalizeIdentifier('привет')).toBe('привет')
|
||||||
|
expect(normalizeIdentifier('こんにちは')).toBe('こんにちは')
|
||||||
|
expect(normalizeIdentifier('مرحبا')).toBe('مرحبا')
|
||||||
|
})
|
||||||
|
})
|
|
@ -0,0 +1,19 @@
|
||||||
|
export function normalizeIdentifier(name: string): string {
|
||||||
|
// Trim and lowercase
|
||||||
|
name = name.trim().toLowerCase()
|
||||||
|
|
||||||
|
// Normalize Unicode to NFKC form
|
||||||
|
name = name.normalize('NFKC')
|
||||||
|
|
||||||
|
// Convert to array of characters and map each one
|
||||||
|
return Array.from(name)
|
||||||
|
.map(char => {
|
||||||
|
// Check if character is letter or number using Unicode ranges
|
||||||
|
if (/\p{Letter}/u.test(char) || /\p{Number}/u.test(char)) {
|
||||||
|
return char
|
||||||
|
}
|
||||||
|
|
||||||
|
return '-'
|
||||||
|
})
|
||||||
|
.join('')
|
||||||
|
}
|
|
@ -173,6 +173,11 @@
|
||||||
"require": "./lib/cjs/nip49.js",
|
"require": "./lib/cjs/nip49.js",
|
||||||
"types": "./lib/types/nip49.d.ts"
|
"types": "./lib/types/nip49.d.ts"
|
||||||
},
|
},
|
||||||
|
"./nip54": {
|
||||||
|
"import": "./lib/esm/nip54.js",
|
||||||
|
"require": "./lib/cjs/nip54.js",
|
||||||
|
"types": "./lib/types/nip54.d.ts"
|
||||||
|
},
|
||||||
"./nip57": {
|
"./nip57": {
|
||||||
"import": "./lib/esm/nip57.js",
|
"import": "./lib/esm/nip57.js",
|
||||||
"require": "./lib/cjs/nip57.js",
|
"require": "./lib/cjs/nip57.js",
|
||||||
|
|
Loading…
Reference in New Issue