From dcf101c6c20c71bd161b1d6bd3ace5da6418a975 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Apr 2023 18:33:09 -0500 Subject: [PATCH] yarn format --- nip21.test.js | 32 +++++++++++++++++++++++++------- nip21.ts | 10 +++++++--- nip27.test.js | 39 ++++++++++++++++++++++++--------------- nip27.ts | 14 +++++++++----- 4 files changed, 65 insertions(+), 30 deletions(-) diff --git a/nip21.test.js b/nip21.test.js index 6712f58..acc9562 100644 --- a/nip21.test.js +++ b/nip21.test.js @@ -2,23 +2,41 @@ const {nip21} = require('./lib/nostr.cjs') test('test', () => { - expect(nip21.test('nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6')).toBe(true) - expect(nip21.test('nostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky')).toBe(true) - expect(nip21.test(' nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6')).toBe(false) + expect( + nip21.test( + 'nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6' + ) + ).toBe(true) + expect( + nip21.test( + 'nostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky' + ) + ).toBe(true) + expect( + nip21.test( + ' nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6' + ) + ).toBe(false) expect(nip21.test('nostr:')).toBe(false) - expect(nip21.test('nostr:npub108pv4cg5ag52nQq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6')).toBe(false) + expect( + nip21.test( + 'nostr:npub108pv4cg5ag52nQq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6' + ) + ).toBe(false) expect(nip21.test('gggggg')).toBe(false) }) test('parse', () => { - const result = nip21.parse('nostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky') + const result = nip21.parse( + 'nostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky' + ) expect(result).toEqual({ uri: 'nostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky', value: 'note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky', decoded: { type: 'note', - data: '46d731680add2990efe1cc619dc9b8014feeb23261ab9dee50e9d11814de5a2b', - }, + data: '46d731680add2990efe1cc619dc9b8014feeb23261ab9dee50e9d11814de5a2b' + } }) }) diff --git a/nip21.ts b/nip21.ts index 06692c6..a5608a2 100644 --- a/nip21.ts +++ b/nip21.ts @@ -5,14 +5,18 @@ import * as nip21 from './nip21' * Bech32 regex. * @see https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32 */ -export const BECH32_REGEX = /[\x21-\x7E]{1,83}1[023456789acdefghjklmnpqrstuvwxyz]{6,}/ +export const BECH32_REGEX = + /[\x21-\x7E]{1,83}1[023456789acdefghjklmnpqrstuvwxyz]{6,}/ /** Nostr URI regex, eg `nostr:npub1...` */ export const NOSTR_URI_REGEX = new RegExp(`nostr:(${BECH32_REGEX.source})`) /** Test whether the value is a Nostr URI. */ export function test(value: unknown): value is `nostr:${string}` { - return typeof value === 'string' && new RegExp(`^${NOSTR_URI_REGEX.source}$`).test(value) + return ( + typeof value === 'string' && + new RegExp(`^${NOSTR_URI_REGEX.source}$`).test(value) + ) } /** Parsed Nostr URI data. */ @@ -32,6 +36,6 @@ export function parse(uri: string): NostrURI { return { uri: match[0] as `nostr:${string}`, value: match[1], - decoded: nip19.decode(match[1]), + decoded: nip19.decode(match[1]) } } diff --git a/nip27.test.js b/nip27.test.js index 9e29906..f7436fd 100644 --- a/nip27.test.js +++ b/nip27.test.js @@ -3,29 +3,38 @@ const {nip27} = require('./lib/nostr.cjs') test('find', () => { const result = nip27.find( - 'Hello nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6!\n\nnostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky', + 'Hello nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6!\n\nnostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky' ) - expect(result).toEqual([{ - uri: 'nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6', - value: 'npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6', - decoded: { type: 'npub', data: '79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6' }, - start: 6, - end: 75, - }, { - uri: 'nostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky', - value: 'note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky', - decoded: { type: 'note', data: '46d731680add2990efe1cc619dc9b8014feeb23261ab9dee50e9d11814de5a2b' }, - start: 78, - end: 147, - }]) + expect(result).toEqual([ + { + uri: 'nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6', + value: 'npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6', + decoded: { + type: 'npub', + data: '79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6' + }, + start: 6, + end: 75 + }, + { + uri: 'nostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky', + value: 'note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky', + decoded: { + type: 'note', + data: '46d731680add2990efe1cc619dc9b8014feeb23261ab9dee50e9d11814de5a2b' + }, + start: 78, + end: 147 + } + ]) }) test('replaceAll', () => { const content = 'Hello nostr:npub108pv4cg5ag52nq082kd5leu9ffrn2gdg6g4xdwatn73y36uzplmq9uyev6!\n\nnostr:note1gmtnz6q2m55epmlpe3semjdcq987av3jvx4emmjsa8g3s9x7tg4sclreky' - const result = nip27.replaceAll(content, ({ decoded, value }) => { + const result = nip27.replaceAll(content, ({decoded, value}) => { switch (decoded.type) { case 'npub': return '@alex' diff --git a/nip27.ts b/nip27.ts index e415839..5ebd49f 100644 --- a/nip27.ts +++ b/nip27.ts @@ -2,7 +2,8 @@ import * as nip19 from './nip19' import * as nip21 from './nip21' /** Regex to find NIP-21 URIs inside event content. */ -export const regex = () => new RegExp(`\\b${nip21.NOSTR_URI_REGEX.source}\\b`, 'g') +export const regex = () => + new RegExp(`\\b${nip21.NOSTR_URI_REGEX.source}\\b`, 'g') /** Match result for a Nostr URI in event content. */ export interface NostrURIMatch extends nip21.NostrURI { @@ -16,7 +17,7 @@ export interface NostrURIMatch extends nip21.NostrURI { export function find(content: string): NostrURIMatch[] { const matches = content.matchAll(regex()) - return [...matches].map((match) => { + return [...matches].map(match => { const [uri, value] = match return { @@ -24,7 +25,7 @@ export function find(content: string): NostrURIMatch[] { value, decoded: nip19.decode(value), start: match.index!, - end: match.index! + uri.length, + end: match.index! + uri.length } }) } @@ -48,12 +49,15 @@ export function find(content: string): NostrURIMatch[] { * }) * ``` */ -export function replaceAll(content: string, replacer: (match: nip21.NostrURI) => string): string { +export function replaceAll( + content: string, + replacer: (match: nip21.NostrURI) => string +): string { return content.replaceAll(regex(), (uri, value) => { return replacer({ uri: uri as `nostr:${string}`, value, - decoded: nip19.decode(value), + decoded: nip19.decode(value) }) }) }