@helios-lang/effect 0.1.0

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 (64) hide show
  1. package/LICENSE +28 -0
  2. package/README.md +3 -0
  3. package/dist/Address.js +13 -0
  4. package/dist/Address.js.map +1 -0
  5. package/dist/Bech32.js +153 -0
  6. package/dist/Bech32.js.map +1 -0
  7. package/dist/Cbor.js +1171 -0
  8. package/dist/Cbor.js.map +1 -0
  9. package/dist/Uplc/Cek.js +3 -0
  10. package/dist/Uplc/Cek.js.map +1 -0
  11. package/dist/Uplc/Data.js +171 -0
  12. package/dist/Uplc/Data.js.map +1 -0
  13. package/dist/Uplc/DataSchema.js +118 -0
  14. package/dist/Uplc/DataSchema.js.map +1 -0
  15. package/dist/Uplc/Primitive.js +23 -0
  16. package/dist/Uplc/Primitive.js.map +1 -0
  17. package/dist/Uplc/index.js +4 -0
  18. package/dist/Uplc/index.js.map +1 -0
  19. package/dist/index.js +5 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/internal/Base32.js +201 -0
  22. package/dist/internal/Base32.js.map +1 -0
  23. package/dist/internal/BigEndian.js +56 -0
  24. package/dist/internal/BigEndian.js.map +1 -0
  25. package/dist/internal/Bits.js +300 -0
  26. package/dist/internal/Bits.js.map +1 -0
  27. package/dist/internal/Bytes.js +293 -0
  28. package/dist/internal/Bytes.js.map +1 -0
  29. package/dist/internal/Flat.js +298 -0
  30. package/dist/internal/Flat.js.map +1 -0
  31. package/dist/internal/Float.js +154 -0
  32. package/dist/internal/Float.js.map +1 -0
  33. package/dist/internal/Utf8.js +44 -0
  34. package/dist/internal/Utf8.js.map +1 -0
  35. package/eslint.config.mjs +30 -0
  36. package/package.json +36 -0
  37. package/src/Address.ts +20 -0
  38. package/src/Bech32.test.ts +117 -0
  39. package/src/Bech32.ts +198 -0
  40. package/src/Cbor.test.ts +1610 -0
  41. package/src/Cbor.ts +1704 -0
  42. package/src/Uplc/Cek.ts +92 -0
  43. package/src/Uplc/Data.ts +259 -0
  44. package/src/Uplc/DataSchema.test.ts +207 -0
  45. package/src/Uplc/DataSchema.ts +181 -0
  46. package/src/Uplc/Primitive.ts +56 -0
  47. package/src/Uplc/index.ts +3 -0
  48. package/src/index.ts +4 -0
  49. package/src/internal/Base32.test.ts +219 -0
  50. package/src/internal/Base32.ts +341 -0
  51. package/src/internal/BigEndian.test.ts +79 -0
  52. package/src/internal/BigEndian.ts +67 -0
  53. package/src/internal/Bits.test.ts +300 -0
  54. package/src/internal/Bits.ts +398 -0
  55. package/src/internal/Bytes.test.ts +369 -0
  56. package/src/internal/Bytes.ts +343 -0
  57. package/src/internal/Flat.test.ts +29 -0
  58. package/src/internal/Flat.ts +387 -0
  59. package/src/internal/Float.test.ts +51 -0
  60. package/src/internal/Float.ts +190 -0
  61. package/src/internal/Utf8.test.ts +69 -0
  62. package/src/internal/Utf8.ts +58 -0
  63. package/tsconfig.build.json +14 -0
  64. package/tsconfig.json +38 -0
@@ -0,0 +1,58 @@
1
+ import { Effect, Encoding } from "effect"
2
+ import * as Bytes from "./Bytes.js"
3
+
4
+ /**
5
+ * Decodes a list of uint8 bytes into a string using UTF-8 encoding.
6
+ * @example
7
+ * bytesToUtf8([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) == "hello world"
8
+ * @param bytes
9
+ * @returns
10
+ */
11
+ export function decode(
12
+ bytes: string | number[] | Uint8Array
13
+ ): Effect.Effect<string, Encoding.DecodeException> {
14
+ return Effect.sync(() =>
15
+ new TextDecoder("utf-8", { fatal: true }).decode(
16
+ Bytes.toUint8Array(bytes).buffer
17
+ )
18
+ ).pipe(
19
+ Effect.catchAll(() =>
20
+ Effect.fail(Bytes.DecodeException(bytes, "Invalid utf-8 encoding"))
21
+ )
22
+ )
23
+ }
24
+
25
+ /**
26
+ * Encodes a string into a list of uint8 bytes using UTF-8 encoding.
27
+ * @example
28
+ * utf8ToBytes("hello world") == [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
29
+ * @param str
30
+ * @returns
31
+ */
32
+ export function encode(str: string): Uint8Array {
33
+ return new TextEncoder().encode(str)
34
+ }
35
+
36
+ /**
37
+ * Tests if a uint8 array is valid utf8 encoding.
38
+ * @param {number[]} bytes
39
+ * @returns {boolean}
40
+ */
41
+ export function isValid(bytes: string | number[] | Uint8Array): boolean {
42
+ /**
43
+ * Bytes.toArray() doesn't fail if any of the bytes are out of range
44
+ */
45
+ const bs = Bytes.toArray(bytes)
46
+
47
+ if (bs.some((b) => b < 0 || b > 255)) {
48
+ return false
49
+ }
50
+
51
+ try {
52
+ new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array(bs).buffer)
53
+
54
+ return true
55
+ } catch (_e) {
56
+ return false
57
+ }
58
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "noEmit": false,
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "declarationDir": "./types",
9
+ "sourceMap": true,
10
+ "removeComments": false
11
+ },
12
+ "exclude": ["node_modules", "**/*.d.ts", "**/*.test.ts"],
13
+ "include": ["src/**/*.ts"]
14
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "compilerOptions": {
3
+ "strict": true,
4
+ "rootDir": "src",
5
+ "declarationMap": false,
6
+ "noEmit": true,
7
+ "skipLibCheck": true,
8
+ "noImplicitThis": true,
9
+ "noImplicitAny": true,
10
+ "strictNullChecks": true,
11
+ "moduleResolution": "node",
12
+ "module": "esnext",
13
+ "target": "esnext",
14
+ "lib": ["esnext"],
15
+ "exactOptionalPropertyTypes": true,
16
+ "moduleDetection": "force",
17
+ "composite": true,
18
+ "downlevelIteration": true,
19
+ "esModuleInterop": false,
20
+ "emitDecoratorMetadata": true,
21
+ "experimentalDecorators": true,
22
+ "types": ["bun"],
23
+ "isolatedModules": true,
24
+ "noImplicitReturns": false,
25
+ "noUnusedLocals": true,
26
+ "noUnusedParameters": false,
27
+ "noFallthroughCasesInSwitch": true,
28
+ "noEmitOnError": false,
29
+ "noErrorTruncation": false,
30
+ "allowJs": false,
31
+ "checkJs": false,
32
+ "forceConsistentCasingInFileNames": true,
33
+ "noUncheckedIndexedAccess": false,
34
+ "incremental": true
35
+ },
36
+ "exclude": ["node_modules", "**/*.d.ts"],
37
+ "include": ["src/**/*.ts"]
38
+ }