@atproto/lex-data 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/blob.d.ts +39 -5
  3. package/dist/blob.d.ts.map +1 -1
  4. package/dist/blob.js +45 -16
  5. package/dist/blob.js.map +1 -1
  6. package/dist/cid.d.ts +83 -14
  7. package/dist/cid.d.ts.map +1 -1
  8. package/dist/cid.js +94 -35
  9. package/dist/cid.js.map +1 -1
  10. package/dist/lex-equals.js +1 -1
  11. package/dist/lex-equals.js.map +1 -1
  12. package/dist/uint8array-from-base64.d.ts.map +1 -1
  13. package/dist/uint8array-from-base64.js +1 -1
  14. package/dist/uint8array-from-base64.js.map +1 -1
  15. package/dist/uint8array-to-base64.d.ts.map +1 -1
  16. package/dist/uint8array-to-base64.js +2 -2
  17. package/dist/uint8array-to-base64.js.map +1 -1
  18. package/dist/utf8-from-base64.d.ts +4 -0
  19. package/dist/utf8-from-base64.d.ts.map +1 -0
  20. package/dist/utf8-from-base64.js +18 -0
  21. package/dist/utf8-from-base64.js.map +1 -0
  22. package/dist/utf8-to-base64.d.ts +4 -0
  23. package/dist/utf8-to-base64.d.ts.map +1 -0
  24. package/dist/utf8-to-base64.js +20 -0
  25. package/dist/utf8-to-base64.js.map +1 -0
  26. package/dist/utf8.d.ts +3 -0
  27. package/dist/utf8.d.ts.map +1 -1
  28. package/dist/utf8.js +16 -3
  29. package/dist/utf8.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/blob.test.ts +150 -25
  32. package/src/blob.ts +111 -27
  33. package/src/cid.test.ts +50 -33
  34. package/src/cid.ts +200 -35
  35. package/src/lex-equals.ts +2 -2
  36. package/src/uint8array-from-base64.ts +1 -1
  37. package/src/uint8array-to-base64.test.ts +2 -2
  38. package/src/uint8array-to-base64.ts +2 -2
  39. package/src/utf8-from-base64.test.ts +39 -0
  40. package/src/utf8-from-base64.ts +23 -0
  41. package/src/utf8-grapheme-len.test.ts +2 -2
  42. package/src/utf8-len.test.ts +2 -2
  43. package/src/utf8-to-base64.test.ts +35 -0
  44. package/src/utf8-to-base64.ts +22 -0
  45. package/src/utf8.ts +23 -2
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, it } from 'vitest'
2
2
  import { graphemeLenNative, graphemeLenPonyfill } from './utf8-grapheme-len.js'
3
3
 
4
- describe('graphemeLenSegmenter', () => {
4
+ describe(graphemeLenNative!, () => {
5
5
  it('computes grapheme length', () => {
6
6
  expect(graphemeLenNative!('a')).toBe(1)
7
7
  expect(graphemeLenNative!('~')).toBe(1)
@@ -19,7 +19,7 @@ describe('graphemeLenSegmenter', () => {
19
19
  })
20
20
  })
21
21
 
22
- describe('graphemeLenInternal', () => {
22
+ describe(graphemeLenPonyfill, () => {
23
23
  it('computes grapheme length', () => {
24
24
  expect(graphemeLenPonyfill('a')).toBe(1)
25
25
  expect(graphemeLenPonyfill('~')).toBe(1)
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, it } from 'vitest'
2
2
  import { utf8LenCompute, utf8LenNode } from './utf8-len.js'
3
3
 
4
- describe('utf8LenNode', () => {
4
+ describe(utf8LenNode!, () => {
5
5
  it('computes utf8 string length', () => {
6
6
  expect(utf8LenNode!('a')).toBe(1)
7
7
  expect(utf8LenNode!('~')).toBe(1)
@@ -16,7 +16,7 @@ describe('utf8LenNode', () => {
16
16
  })
17
17
  })
18
18
 
19
- describe('utf8LenInternal', () => {
19
+ describe(utf8LenCompute, () => {
20
20
  it('computes utf8 string length', () => {
21
21
  expect(utf8LenCompute('a')).toBe(1)
22
22
  expect(utf8LenCompute('~')).toBe(1)
@@ -0,0 +1,35 @@
1
+ import { assert, describe, expect, it } from 'vitest'
2
+ import { utf8ToBase64Node, utf8ToBase64Ponyfill } from './utf8-to-base64.js'
3
+
4
+ const strings = [
5
+ 'Hello, World!',
6
+ '¡Hola, Mundo!',
7
+ 'こんにちは世界',
8
+ '😀👩‍💻🌍',
9
+ '',
10
+ '𓀀𓁐𓂀𓃰𓄿𓅱𓆑𓇋𓈖𓉔𓊃𓋴𓌳𓍿𓎛𓏏',
11
+ ]
12
+
13
+ for (const utf8ToBase64 of [utf8ToBase64Node, utf8ToBase64Ponyfill] as const) {
14
+ assert(utf8ToBase64, 'implementation should not be null')
15
+
16
+ describe(utf8ToBase64, () => {
17
+ it('encodes utf8 string to base64', () => {
18
+ for (const text of strings) {
19
+ const b64 = Buffer.from(text, 'utf8')
20
+ .toString('base64')
21
+ .replaceAll('=', '') // utf8ToBase64 omits padding
22
+ const encoded = utf8ToBase64(text, 'base64')
23
+ expect(encoded).toBe(b64)
24
+ }
25
+ })
26
+
27
+ it('encodes utf8 string to base64url', () => {
28
+ for (const text of strings) {
29
+ const b64u = Buffer.from(text, 'utf8').toString('base64url')
30
+ const encoded = utf8ToBase64(text, 'base64url')
31
+ expect(encoded).toBe(b64u)
32
+ }
33
+ })
34
+ })
35
+ }
@@ -0,0 +1,22 @@
1
+ import { toString } from 'uint8arrays/to-string'
2
+ import { NodeJSBuffer } from './lib/nodejs-buffer.js'
3
+ import { Base64Alphabet } from './uint8array-base64.js'
4
+ import { toBase64Node } from './uint8array-to-base64.js'
5
+
6
+ const Buffer = NodeJSBuffer
7
+
8
+ export const utf8ToBase64Node = Buffer
9
+ ? function utf8ToBase64Node(text: string, alphabet?: Base64Alphabet): string {
10
+ const buffer = Buffer.from(text, 'utf8')
11
+ return toBase64Node!(buffer, alphabet)
12
+ }
13
+ : /* v8 ignore next -- @preserve */ null
14
+
15
+ const textEncoder = /*#__PURE__*/ new TextEncoder()
16
+ export function utf8ToBase64Ponyfill(
17
+ text: string,
18
+ alphabet?: Base64Alphabet,
19
+ ): string {
20
+ const bytes = textEncoder.encode(text)
21
+ return toString(bytes, alphabet)
22
+ }
package/src/utf8.ts CHANGED
@@ -1,9 +1,17 @@
1
+ import { Base64Alphabet } from './uint8array.js'
2
+ import {
3
+ utf8FromBase64Node,
4
+ utf8FromBase64Ponyfill,
5
+ } from './utf8-from-base64.js'
1
6
  import { graphemeLenNative, graphemeLenPonyfill } from './utf8-grapheme-len.js'
2
7
  import { utf8LenCompute, utf8LenNode } from './utf8-len.js'
8
+ import { utf8ToBase64Node, utf8ToBase64Ponyfill } from './utf8-to-base64.js'
3
9
 
4
10
  export const graphemeLen: (str: string) => number =
5
- graphemeLenNative ?? graphemeLenPonyfill
11
+ /* v8 ignore next -- @preserve */ graphemeLenNative ??
12
+ /* v8 ignore next -- @preserve */ graphemeLenPonyfill
6
13
 
14
+ /* v8 ignore next -- @preserve */
7
15
  if (graphemeLen === graphemeLenPonyfill) {
8
16
  /*#__PURE__*/
9
17
  console.warn(
@@ -11,4 +19,17 @@ if (graphemeLen === graphemeLenPonyfill) {
11
19
  )
12
20
  }
13
21
 
14
- export const utf8Len: (string: string) => number = utf8LenNode ?? utf8LenCompute
22
+ export const utf8Len: (string: string) => number =
23
+ /* v8 ignore next -- @preserve */ utf8LenNode ??
24
+ /* v8 ignore next -- @preserve */ utf8LenCompute
25
+
26
+ export const utf8ToBase64: (str: string, alphabet?: Base64Alphabet) => string =
27
+ /* v8 ignore next -- @preserve */ utf8ToBase64Node ??
28
+ /* v8 ignore next -- @preserve */ utf8ToBase64Ponyfill
29
+
30
+ export const utf8FromBase64: (
31
+ b64: string,
32
+ alphabet?: Base64Alphabet,
33
+ ) => string =
34
+ /* v8 ignore next -- @preserve */ utf8FromBase64Node ??
35
+ /* v8 ignore next -- @preserve */ utf8FromBase64Ponyfill