j-templates 7.0.74 → 7.0.76

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,11 +1,9 @@
1
1
  export type EmitterCallback<T extends readonly any[] = any[]> = (...args: T) => void;
2
- export type Emitter = [number, ...EmitterCallback[]];
2
+ export type Emitter = EmitterCallback[];
3
3
  export declare namespace Emitter {
4
4
  function Create(): Emitter;
5
- function GetId(emitter: Emitter): number;
6
5
  function On(emitter: Emitter, callback: EmitterCallback): void;
7
6
  function Emit(emitter: Emitter, ...args: any[]): void;
8
- function Destroy(emitter: Emitter): void;
9
7
  function Remove(emitter: Emitter, callback: EmitterCallback): void;
10
8
  function Clear(emitter: Emitter): void;
11
9
  }
package/Utils/emitter.js CHANGED
@@ -1,30 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Emitter = void 0;
4
- const idPool = [];
5
- let idPoolIndex = 0;
6
- let globalId = 0;
7
4
  var Emitter;
8
5
  (function (Emitter) {
9
6
  function Create() {
10
- const emitterId = idPoolIndex === 0 ? ++globalId : idPool[--idPoolIndex];
11
- const emitter = [emitterId];
12
- return emitter;
7
+ return [];
13
8
  }
14
9
  Emitter.Create = Create;
15
- function GetId(emitter) {
16
- return emitter[0];
17
- }
18
- Emitter.GetId = GetId;
19
10
  function On(emitter, callback) {
20
11
  emitter.push(callback);
21
12
  }
22
13
  Emitter.On = On;
23
14
  function Emit(emitter, ...args) {
24
- if (emitter[0] === -1)
25
- return;
26
- let writePos = 1;
27
- for (let x = 1; x < emitter.length; x++) {
15
+ let writePos = 0;
16
+ for (let x = 0; x < emitter.length; x++) {
28
17
  if (emitter[x] !== null) {
29
18
  emitter[x](...args);
30
19
  emitter[writePos++] = emitter[x];
@@ -34,21 +23,14 @@ var Emitter;
34
23
  emitter.splice(writePos);
35
24
  }
36
25
  Emitter.Emit = Emit;
37
- function Destroy(emitter) {
38
- idPool[idPoolIndex++] = emitter[0];
39
- emitter[0] = -1;
40
- }
41
- Emitter.Destroy = Destroy;
42
26
  function Remove(emitter, callback) {
43
- if (emitter[0] === -1)
44
- return;
45
27
  const index = emitter.indexOf(callback);
46
- if (index >= 1)
28
+ if (index >= 0)
47
29
  emitter[index] = null;
48
30
  }
49
31
  Emitter.Remove = Remove;
50
32
  function Clear(emitter) {
51
- emitter.splice(1);
33
+ emitter.splice(0);
52
34
  }
53
35
  Emitter.Clear = Clear;
54
36
  })(Emitter || (exports.Emitter = Emitter = {}));
@@ -1 +1,6 @@
1
+ /**
2
+ * Checks if a function is an async function.
3
+ * @param func - The function to check
4
+ * @returns true if the function is async, false otherwise
5
+ */
1
6
  export declare function IsAsync(func: Function): boolean;
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IsAsync = IsAsync;
4
+ /**
5
+ * Checks if a function is an async function.
6
+ * @param func - The function to check
7
+ * @returns true if the function is async, false otherwise
8
+ */
4
9
  function IsAsync(func) {
5
10
  return func[Symbol.toStringTag] === "AsyncFunction";
6
11
  }
package/Utils/json.d.ts CHANGED
@@ -1,8 +1,18 @@
1
+ /**
2
+ * Result of a JSON diff operation, containing paths and values that changed.
3
+ * @template T - The type of the JSON value being diffed
4
+ */
1
5
  export type JsonDiffResult<T = unknown> = {
6
+ /** Array path segments (strings for object keys, numbers for array indices) */
2
7
  path: (string | number)[];
8
+ /** The new value at this path */
3
9
  value: unknown;
4
10
  }[];
5
11
  export type JsonDiffFactoryResult = ReturnType<typeof JsonDiffFactory>;
12
+ /**
13
+ * Factory that creates JSON utility functions for diffing, merging, and cloning.
14
+ * @returns Object containing JsonDiff, JsonType, JsonDeepClone, and JsonMerge functions
15
+ */
6
16
  export declare function JsonDiffFactory(): {
7
17
  JsonDiff: <T>(newValue: T, oldValue: T, rootPath?: string, initResult?: JsonDiffResult<T>) => JsonDiffResult<T>;
8
18
  JsonType: (value: any) => "object" | "value" | "array";
package/Utils/json.js CHANGED
@@ -3,12 +3,21 @@ var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.JsonMerge = exports.JsonDeepClone = exports.JsonType = exports.JsonDiff = void 0;
5
5
  exports.JsonDiffFactory = JsonDiffFactory;
6
+ /**
7
+ * Factory that creates JSON utility functions for diffing, merging, and cloning.
8
+ * @returns Object containing JsonDiff, JsonType, JsonDeepClone, and JsonMerge functions
9
+ */
6
10
  function JsonDiffFactory() {
7
11
  const jsonProto = Object.getPrototypeOf({});
8
12
  const arrayProto = Object.getPrototypeOf([]);
9
13
  const strProto = Object.getPrototypeOf("");
10
14
  const numProto = Object.getPrototypeOf(0);
11
15
  const boolProto = Object.getPrototypeOf(false);
16
+ /**
17
+ * Determines the JSON type of a value.
18
+ * @param value - The value to check
19
+ * @returns "value" for primitives/null/undefined, "object" for plain objects, "array" for arrays
20
+ */
12
21
  function JsonType(value) {
13
22
  if (value === null || value === undefined)
14
23
  return "value";
@@ -26,6 +35,15 @@ function JsonDiffFactory() {
26
35
  return "array";
27
36
  return "value";
28
37
  }
38
+ /**
39
+ * Deep merges a patch into a source value.
40
+ * - If types don't match, returns the patch
41
+ * - For objects: recursively merges properties, adds new properties
42
+ * - For arrays: maps over patch, merging with corresponding source elements
43
+ * @param source - The original value
44
+ * @param patch - The value to merge into source
45
+ * @returns The merged result
46
+ */
29
47
  function JsonMerge(source, patch) {
30
48
  if (patch === undefined)
31
49
  return JsonDeepClone(source);
@@ -46,7 +64,7 @@ function JsonDiffFactory() {
46
64
  const typedSource = source;
47
65
  const typedPatch = patch;
48
66
  const sourceKeys = Object.keys(typedSource);
49
- const targetKeys = Object.keys(typedPatch).filter(key => !sourceKeys.includes(key));
67
+ const targetKeys = Object.keys(typedPatch).filter((key) => !sourceKeys.includes(key));
50
68
  const result = {};
51
69
  for (let x = 0; x < sourceKeys.length; x++) {
52
70
  result[sourceKeys[x]] = JsonMerge(typedSource[sourceKeys[x]], typedPatch[sourceKeys[x]]);
@@ -60,6 +78,12 @@ function JsonDiffFactory() {
60
78
  return patch;
61
79
  }
62
80
  }
81
+ /**
82
+ * Creates a deep clone of a JSON-compatible value.
83
+ * @template T - The type of the value to clone
84
+ * @param value - The value to clone
85
+ * @returns A deep clone of the value
86
+ */
63
87
  function JsonDeepClone(value) {
64
88
  const type = JsonType(value);
65
89
  switch (type) {
@@ -81,12 +105,31 @@ function JsonDiffFactory() {
81
105
  return value;
82
106
  }
83
107
  }
108
+ /**
109
+ * Computes the differences between two JSON values.
110
+ * Returns an array of paths and values that changed from oldValue to newValue.
111
+ * @template T - The type of the JSON values being compared
112
+ * @param newValue - The new value
113
+ * @param oldValue - The old value to compare against
114
+ * @param rootPath - Optional dot-separated string for the base path (for internal recursion)
115
+ * @param initResult - Optional initial result array (for internal recursion)
116
+ * @returns Array of change records with path and new value
117
+ */
84
118
  function JsonDiff(newValue, oldValue, rootPath, initResult) {
85
119
  const result = initResult ?? [];
86
120
  const startPath = rootPath ? rootPath.split(".") : [];
87
121
  JsonDiffRecursive(startPath, newValue, oldValue, result);
88
122
  return result;
89
123
  }
124
+ /**
125
+ * Recursive helper for JsonDiff that traverses and compares values.
126
+ * @internal
127
+ * @param path - Current path being evaluated
128
+ * @param newValue - New value at this path
129
+ * @param oldValue - Old value at this path
130
+ * @param resp - Result array to populate with changes
131
+ * @returns true if the entire subtree changed, false otherwise
132
+ */
90
133
  function JsonDiffRecursive(path, newValue, oldValue, resp) {
91
134
  if (newValue === oldValue)
92
135
  return false;
@@ -115,6 +158,15 @@ function JsonDiffFactory() {
115
158
  }
116
159
  return false;
117
160
  }
161
+ /**
162
+ * Compares two arrays and records differences.
163
+ * @internal
164
+ * @param path - Current path being evaluated
165
+ * @param newValue - New array
166
+ * @param oldValue - Old array to compare against
167
+ * @param resp - Result array to populate with changes
168
+ * @returns true if the entire array changed, false otherwise
169
+ */
118
170
  function JsonDiffArrays(path, newValue, oldValue, resp) {
119
171
  if (oldValue.length === 0 || newValue.length === 0) {
120
172
  return oldValue.length !== newValue.length;
@@ -138,6 +190,16 @@ function JsonDiffFactory() {
138
190
  allChildrenChanged = false;
139
191
  return allChildrenChanged;
140
192
  }
193
+ /**
194
+ * Compares two objects and records differences.
195
+ * Uses sorted keys and two-pointer technique for efficient comparison.
196
+ * @internal
197
+ * @param path - Current path being evaluated
198
+ * @param newValue - New object
199
+ * @param oldValue - Old object to compare against
200
+ * @param resp - Result array to populate with changes
201
+ * @returns true if the entire object should be replaced (keys removed), false otherwise
202
+ */
141
203
  function JsonDiffObjects(path, newValue, oldValue, resp) {
142
204
  const newKeys = Object.keys(newValue).sort();
143
205
  const oldKeys = Object.keys(oldValue).sort();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.74",
3
+ "version": "7.0.76",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",
package/Utils/bitSet.d.ts DELETED
@@ -1,23 +0,0 @@
1
- export type BitSet = {
2
- init: boolean;
3
- buffer: Uint32Array | null;
4
- activeFrames: Set<number>;
5
- minFrame: number;
6
- maxFrame: number;
7
- };
8
- export type BitSetPool = {
9
- pool: BitSet[];
10
- index: number;
11
- };
12
- export declare namespace BitSetPool {
13
- function Create(): BitSetPool;
14
- function Get(pool: BitSetPool): BitSet;
15
- function Return(pool: BitSetPool, bitSet: BitSet): void;
16
- }
17
- export declare namespace BitSet {
18
- function Create(): BitSet;
19
- function set(set: BitSet, num: number): void;
20
- function has(set: BitSet, num: number): boolean;
21
- function add(set: BitSet, num: number): boolean;
22
- function remove(set: BitSet, num: number): boolean;
23
- }
package/Utils/bitSet.js DELETED
@@ -1,139 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BitSet = exports.BitSetPool = void 0;
4
- // 100000 = 31
5
- const FRAME_BIT_SHIFT = 5;
6
- const DEFAULT_FRAME_COUNT = 1024;
7
- const DEFAULT_FRAME_HALF = DEFAULT_FRAME_COUNT / 2;
8
- function GetFrame(number) {
9
- // Effectively equivalent to Math.floor(number / 32)
10
- return number >>> FRAME_BIT_SHIFT;
11
- }
12
- function GetBit(number) {
13
- // JavaScript only uses rightmost 5 bits of num for this operation
14
- // Effectively equivalent to number % 32
15
- return 1 << number;
16
- }
17
- function Resize(set, frame) {
18
- if (set.minFrame <= frame && frame < set.maxFrame)
19
- return;
20
- const currentSize = set.maxFrame - set.minFrame;
21
- let newMin = Math.min(set.minFrame, frame);
22
- let newMax = Math.max(set.maxFrame, frame + 1);
23
- let newSize = newMax - newMin;
24
- const minimumNewSize = currentSize * 2;
25
- if (newSize < minimumNewSize) {
26
- const growBy = minimumNewSize - newSize;
27
- const growByHalf = growBy / 2;
28
- newMin = newMin - Math.floor(growByHalf);
29
- newMax = newMax + Math.ceil(growByHalf);
30
- if (newMin < 0) {
31
- newMax -= newMin;
32
- newMin = 0;
33
- }
34
- newSize = newMax - newMin;
35
- }
36
- const newBuffer = new Uint32Array(newSize);
37
- newBuffer.set(set.buffer, set.minFrame - newMin);
38
- set.buffer = newBuffer;
39
- set.minFrame = newMin;
40
- set.maxFrame = newMax;
41
- }
42
- function Initialize(set, initFrameNumber) {
43
- if (set.init)
44
- return;
45
- set.init = true;
46
- set.activeFrames.clear();
47
- const frame = GetFrame(initFrameNumber);
48
- const sizeHalf = (set.buffer?.length ?? DEFAULT_FRAME_COUNT) / 2;
49
- set.buffer ??= new Uint32Array(DEFAULT_FRAME_COUNT);
50
- set.minFrame = frame - Math.floor(sizeHalf);
51
- set.maxFrame = frame + Math.ceil(sizeHalf);
52
- if (set.minFrame < 0) {
53
- set.maxFrame -= set.minFrame;
54
- set.minFrame = 0;
55
- }
56
- }
57
- var BitSetPool;
58
- (function (BitSetPool) {
59
- function Create() {
60
- return {
61
- pool: [],
62
- index: 0
63
- };
64
- }
65
- BitSetPool.Create = Create;
66
- function Get(pool) {
67
- return pool.index === 0 ? BitSet.Create() : pool.pool[--pool.index];
68
- }
69
- BitSetPool.Get = Get;
70
- function Return(pool, bitSet) {
71
- bitSet.init = false;
72
- pool.pool[pool.index++] = bitSet;
73
- }
74
- BitSetPool.Return = Return;
75
- })(BitSetPool || (exports.BitSetPool = BitSetPool = {}));
76
- var BitSet;
77
- (function (BitSet) {
78
- function Create() {
79
- return {
80
- init: false,
81
- buffer: null,
82
- activeFrames: new Set(),
83
- minFrame: -1,
84
- maxFrame: -1,
85
- };
86
- }
87
- BitSet.Create = Create;
88
- function set(set, num) {
89
- Initialize(set, num);
90
- const frame = GetFrame(num);
91
- Resize(set, frame);
92
- if (!set.activeFrames.has(frame)) {
93
- set.buffer[frame] = 0;
94
- set.activeFrames.add(frame);
95
- }
96
- const frameOffset = frame - set.minFrame;
97
- set.buffer[frameOffset] |= GetBit(num);
98
- }
99
- BitSet.set = set;
100
- function has(set, num) {
101
- if (set.buffer === null)
102
- return false;
103
- const frame = GetFrame(num);
104
- if (!set.activeFrames.has(frame))
105
- return false;
106
- if (frame < set.minFrame || set.maxFrame <= frame)
107
- return false;
108
- const frameOffset = frame - set.minFrame;
109
- const frameValue = set.buffer[frameOffset] & GetBit(num);
110
- return frameValue !== 0;
111
- }
112
- BitSet.has = has;
113
- function add(set, num) {
114
- Initialize(set, num);
115
- const frame = GetFrame(num);
116
- Resize(set, frame);
117
- if (!set.activeFrames.has(frame)) {
118
- set.buffer[frame] = 0;
119
- set.activeFrames.add(frame);
120
- }
121
- const frameOffset = frame - set.minFrame;
122
- const frameStart = set.buffer[frameOffset];
123
- set.buffer[frameOffset] |= GetBit(num);
124
- return frameStart !== set.buffer[frameOffset];
125
- }
126
- BitSet.add = add;
127
- function remove(set, num) {
128
- if (set.buffer === null)
129
- return false;
130
- const frame = GetFrame(num);
131
- if (!set.activeFrames.has(frame) || frame < set.minFrame || set.maxFrame <= frame)
132
- return false;
133
- const frameOffset = frame - set.minFrame;
134
- const frameStart = set.buffer[frameOffset];
135
- set.buffer[frameOffset] ^= GetBit(num);
136
- return frameStart !== set.buffer[frameOffset];
137
- }
138
- BitSet.remove = remove;
139
- })(BitSet || (exports.BitSet = BitSet = {}));
@@ -1 +0,0 @@
1
- export { JsonType } from './json';
package/Utils/jsonType.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JsonType = void 0;
4
- var json_1 = require("./json");
5
- Object.defineProperty(exports, "JsonType", { enumerable: true, get: function () { return json_1.JsonType; } });
package/index.debug.d.ts DELETED
File without changes
package/index.debug.js DELETED
File without changes