functionalscript 0.12.9 → 0.12.10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.12.9",
3
+ "version": "0.12.10",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -163,6 +163,7 @@ export type BitOrder = {
163
163
  readonly cmp: (a: Vec) => (b: Vec) => Sign;
164
164
  readonly unpackSplit: (len: bigint) => (u: Unpacked) => readonly [bigint, bigint];
165
165
  readonly unpackConcat: (a: Unpacked) => (b: Unpacked) => Unpacked;
166
+ readonly startsWith: (prefix: Vec) => (v: Vec) => boolean;
166
167
  };
167
168
  /**
168
169
  * Implements operations for handling vectors in a least-significant-bit (LSb) first order.
@@ -119,6 +119,13 @@ const bo = ({ front, removeFront, norm, uintCmp, unpackSplit, unpackConcatUint }
119
119
  const unpackConcat = (a) => (b) => ({
120
120
  length: a.length + b.length, uint: unpackConcatUint(a)(b)
121
121
  });
122
+ const popFront = len => {
123
+ const f = unpackPopFront(len);
124
+ return v => {
125
+ const [uint, u] = f(unpack(v));
126
+ return [uint, pack(u)];
127
+ };
128
+ };
122
129
  return {
123
130
  front,
124
131
  removeFront,
@@ -129,13 +136,7 @@ const bo = ({ front, removeFront, norm, uintCmp, unpackSplit, unpackConcatUint }
129
136
  },
130
137
  xor: op(norm)(xor),
131
138
  unpackPopFront,
132
- popFront: len => {
133
- const f = unpackPopFront(len);
134
- return v => {
135
- const [uint, u] = f(unpack(v));
136
- return [uint, pack(u)];
137
- };
138
- },
139
+ popFront,
139
140
  norm,
140
141
  cmp: a => b => {
141
142
  const au = unpack(a);
@@ -148,6 +149,10 @@ const bo = ({ front, removeFront, norm, uintCmp, unpackSplit, unpackConcatUint }
148
149
  },
149
150
  unpackSplit,
150
151
  unpackConcat,
152
+ startsWith: prefix => {
153
+ const { length: n, uint: u } = unpack(prefix);
154
+ return v => length(v) < n ? false : popFront(n)(v)[0] === u;
155
+ }
151
156
  };
152
157
  };
153
158
  const lsbUnpackConcatUint = ({ uint: a, length }) => ({ uint: b }) => (b << length) | a;
@@ -39,6 +39,11 @@ declare const _default: {
39
39
  repeat: () => void;
40
40
  lsbCmp: () => void;
41
41
  msbCmp: () => void;
42
+ startsWith: {
43
+ lsb: () => void;
44
+ msb: () => void;
45
+ emptyVec: () => void;
46
+ };
42
47
  u8ListToVec: () => () => void;
43
48
  u8ListUnaligned: () => void;
44
49
  chunkList: {
@@ -458,6 +458,29 @@ export default {
458
458
  c(vec(5n)(0x5n))(vec(4n)(0x5n))(-1); // 0b00101 < 0b0101_
459
459
  c(vec(4n)(0x5n))(vec(5n)(0xan))(-1); // 0b0101_ < 0b01010
460
460
  },
461
+ startsWith: {
462
+ // vector 0xF5 = 0b1111_0101 (8 bits)
463
+ // LSB reads from the low end: bits 0-3 = 0101 = 0x5, bits 4-7 = 1111 = 0xF
464
+ lsb: () => {
465
+ const v = vec(8n)(0xf5n);
466
+ assertEq(lsb.startsWith(vec(4n)(0x5n))(v), true); // low nibble matches
467
+ assertEq(lsb.startsWith(vec(4n)(0xfn))(v), false); // low nibble doesn't match
468
+ assertEq(lsb.startsWith(v)(vec(4n)(0x5n)), false); // prefix longer than vector
469
+ assertEq(lsb.startsWith(empty)(v), true); // empty prefix always matches
470
+ },
471
+ // MSB reads from the high end: bits 0-3 = 1111 = 0xF, bits 4-7 = 0101 = 0x5
472
+ msb: () => {
473
+ const v = vec(8n)(0xf5n);
474
+ assertEq(msb.startsWith(vec(4n)(0xfn))(v), true); // high nibble matches
475
+ assertEq(msb.startsWith(vec(4n)(0x5n))(v), false); // high nibble doesn't match
476
+ assertEq(msb.startsWith(v)(vec(4n)(0xfn)), false); // prefix longer than vector
477
+ assertEq(msb.startsWith(empty)(v), true); // empty prefix always matches
478
+ },
479
+ emptyVec: () => {
480
+ assertEq(lsb.startsWith(empty)(empty), true);
481
+ assertEq(msb.startsWith(empty)(empty), true);
482
+ },
483
+ },
461
484
  u8ListToVec: () => {
462
485
  // 131_072 is too much for Bun
463
486
  const x = u8ListToVec(msb)(listRepeat(0x12)(131_071));
@@ -21,3 +21,4 @@ export declare const join: (_: string) => (input: List<string>) => string;
21
21
  export declare const concat: (input: List<string>) => string;
22
22
  export declare const repeat: (n: string) => (v: number) => string;
23
23
  export declare const cmp: (a: string) => (b: string) => Sign;
24
+ export declare const splitAt: (p: number) => (v: string) => readonly [string, string];
@@ -24,3 +24,4 @@ export const join = compose(joinOp)(reduce);
24
24
  export const concat = reduce(concatOp);
25
25
  export const repeat = v => compose(listRepeat(v))(concat);
26
26
  export const cmp = uCmp;
27
+ export const splitAt = (p) => (v) => [v.substring(0, p), v.substring(p)];
@@ -12,5 +12,12 @@ declare const _default: {
12
12
  };
13
13
  repeat: () => void;
14
14
  cmp: () => void;
15
+ splitAt: {
16
+ middle: () => void;
17
+ zero: () => void;
18
+ full: () => void;
19
+ beyond: () => void;
20
+ empty: () => void;
21
+ };
15
22
  };
16
23
  export default _default;
@@ -1,4 +1,4 @@
1
- import { join, concat, repeat, cmp } from "./module.f.js";
1
+ import { join, concat, repeat, cmp, splitAt } from "./module.f.js";
2
2
  import { repeat as repeatList } from "../list/module.f.js";
3
3
  export default {
4
4
  example: () => {
@@ -67,5 +67,52 @@ export default {
67
67
  if (result !== -1) {
68
68
  throw result;
69
69
  }
70
+ },
71
+ splitAt: {
72
+ middle: () => {
73
+ const [a, b] = splitAt(3)('hello');
74
+ if (a !== 'hel') {
75
+ throw a;
76
+ }
77
+ if (b !== 'lo') {
78
+ throw b;
79
+ }
80
+ },
81
+ zero: () => {
82
+ const [a, b] = splitAt(0)('hello');
83
+ if (a !== '') {
84
+ throw a;
85
+ }
86
+ if (b !== 'hello') {
87
+ throw b;
88
+ }
89
+ },
90
+ full: () => {
91
+ const [a, b] = splitAt(5)('hello');
92
+ if (a !== 'hello') {
93
+ throw a;
94
+ }
95
+ if (b !== '') {
96
+ throw b;
97
+ }
98
+ },
99
+ beyond: () => {
100
+ const [a, b] = splitAt(10)('hello');
101
+ if (a !== 'hello') {
102
+ throw a;
103
+ }
104
+ if (b !== '') {
105
+ throw b;
106
+ }
107
+ },
108
+ empty: () => {
109
+ const [a, b] = splitAt(0)('');
110
+ if (a !== '') {
111
+ throw a;
112
+ }
113
+ if (b !== '') {
114
+ throw b;
115
+ }
116
+ },
70
117
  }
71
118
  };