@based/schema 5.0.0-alpha.25 → 5.0.0-alpha.27

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.
@@ -1,9 +1,11 @@
1
1
  import { getPropType } from '../index.js';
2
2
  import { DEFAULT_MAP } from './defaultMap.js';
3
+ import { fillEmptyMain } from './fillEmptyMain.js';
3
4
  import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, ENUM, NUMBER, } from './types.js';
4
5
  import { getPropLen, isSeparate, parseMinMaxStep } from './utils.js';
5
6
  import { defaultValidation, VALIDATION_MAP } from './validation.js';
6
7
  export const addEdges = (prop, refProp) => {
8
+ const mainEdges = [];
7
9
  for (const key in refProp) {
8
10
  if (key[0] === '$') {
9
11
  if (!prop.edges) {
@@ -21,6 +23,9 @@ export const addEdges = (prop, refProp) => {
21
23
  prop.edgesSeperateCnt++;
22
24
  }
23
25
  const typeIndex = TYPE_INDEX_MAP[edgeType];
26
+ if (edgeProp.default !== undefined) {
27
+ prop.hasDefaultEdges = true;
28
+ }
24
29
  // add default
25
30
  const edge = {
26
31
  __isPropDef: true,
@@ -35,6 +40,9 @@ export const addEdges = (prop, refProp) => {
35
40
  default: edgeProp.default ?? DEFAULT_MAP[typeIndex],
36
41
  start: prop.edgeMainLen,
37
42
  };
43
+ if (!separate) {
44
+ mainEdges.push(edge);
45
+ }
38
46
  if (edgeProp.max !== undefined) {
39
47
  edge.max = parseMinMaxStep(edgeProp.max);
40
48
  }
@@ -72,5 +80,6 @@ export const addEdges = (prop, refProp) => {
72
80
  }
73
81
  }
74
82
  }
83
+ prop.edgeMainEmpty = fillEmptyMain(mainEdges, prop.edgeMainLen);
75
84
  };
76
85
  //# sourceMappingURL=addEdges.js.map
@@ -1,5 +1,5 @@
1
- import { PropDef } from './types.js';
1
+ import { PropDef, PropDefEdge } from './types.js';
2
2
  export declare const ENCODER: TextEncoder;
3
- export declare const fillEmptyMain: (vals: PropDef[], mainLen: number) => Uint8Array<ArrayBuffer>;
3
+ export declare const fillEmptyMain: (vals: (PropDef | PropDefEdge)[], mainLen: number) => Uint8Array<ArrayBuffer>;
4
4
  export declare const isZeroes: (buf: Uint8Array) => boolean;
5
5
  //# sourceMappingURL=fillEmptyMain.d.ts.map
@@ -1,6 +1,5 @@
1
1
  import { convertToTimestamp } from '@saulx/utils';
2
2
  import { BINARY, BOOLEAN, ENUM, INT16, INT32, INT8, NUMBER, STRING, TIMESTAMP, UINT16, UINT32, UINT8, } from './types.js';
3
- // Lets add validation of values in here - need to validate DEFAULT!
4
3
  export const ENCODER = new TextEncoder();
5
4
  export const fillEmptyMain = (vals, mainLen) => {
6
5
  const mainEmpty = new Uint8Array(mainLen);
@@ -12,14 +11,13 @@ export const fillEmptyMain = (vals, mainLen) => {
12
11
  const s = f.start;
13
12
  let val = f.default;
14
13
  if (t === ENUM) {
15
- mainEmpty[s] =
16
- typeof f.default === 'number' ? f.default : f.reverseEnum[val];
14
+ mainEmpty[s] = f.default ?? 0;
17
15
  }
18
16
  else if (t === INT8 || t === UINT8) {
19
17
  mainEmpty[s] = val;
20
18
  }
21
19
  else if (t === BOOLEAN) {
22
- mainEmpty[s] = val === true ? 1 : 0;
20
+ mainEmpty[s] = val ? 1 : 0;
23
21
  }
24
22
  else if (t === UINT32 || t === INT32) {
25
23
  mainEmpty[s] = val;
@@ -86,6 +86,7 @@ export type PropDef = {
86
86
  transform?: Transform;
87
87
  default: any;
88
88
  edgeMainLen?: 0;
89
+ hasDefaultEdges?: boolean;
89
90
  reverseEnum?: {
90
91
  [key: string]: number;
91
92
  };
@@ -2,6 +2,7 @@ import { StrictSchema } from './types.js';
2
2
  type Opts = {
3
3
  readOnly?: boolean;
4
4
  stripMetaInformation?: boolean;
5
+ stripTransform?: boolean;
5
6
  };
6
7
  export declare const serialize: (schema: any, opts?: Opts) => Uint8Array;
7
8
  export declare const deSerializeKey: (buf: Uint8Array, keySize: number, i: number) => {
package/dist/serialize.js CHANGED
@@ -50,8 +50,11 @@ const handleSingleValue = (ops, val, obj, prev, fromObject, key) => {
50
50
  schemaBuffer.len += val.byteLength;
51
51
  }
52
52
  else if (type === 'function') {
53
- const str = val.toString();
54
- // Pessimistically assume 4 bytes per char for UTF-8 to be safe.
53
+ // Support both arrow functions and methods (including shorthand method syntax)
54
+ let str = val.toString();
55
+ if (typeof val === 'function' && /^[a-zA-Z0-9_$]+\s*\(/.test(str)) {
56
+ str = 'function ' + str;
57
+ }
55
58
  ensureCapacity(1 + 2 + str.length * 4);
56
59
  schemaBuffer.buf[schemaBuffer.len] = FUNCTION;
57
60
  schemaBuffer.len += 1;
@@ -103,7 +106,7 @@ const handleSingleValue = (ops, val, obj, prev, fromObject, key) => {
103
106
  schemaBuffer.buf[schemaBuffer.len] = val;
104
107
  schemaBuffer.len += 1;
105
108
  }
106
- else if ((val < 4294967295 || val > 0) && isInt) {
109
+ else if (val < 4294967295 && val > 0 && isInt) {
107
110
  ensureCapacity(5);
108
111
  schemaBuffer.buf[schemaBuffer.len] = UINT32;
109
112
  schemaBuffer.len += 1;
@@ -207,7 +210,13 @@ const walk = (opts, obj, prev, prev2, fromObject, schemaBuffer) => {
207
210
  }
208
211
  else {
209
212
  for (const key in obj) {
210
- if (opts.readOnly &&
213
+ if (opts.stripTransform &&
214
+ isFromObj &&
215
+ key === 'transform' &&
216
+ typeof obj[key] === 'function') {
217
+ continue;
218
+ }
219
+ else if (opts.readOnly &&
211
220
  isFromObj &&
212
221
  (key === 'validation' || key === 'default')) {
213
222
  if (key === 'validation' && typeof obj[key] === 'function') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.25",
3
+ "version": "5.0.0-alpha.27",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",