@softsky/utils 2.3.0 → 2.3.2

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/README.md CHANGED
@@ -67,6 +67,18 @@ Compare must return true when found position to push in.
67
67
  For example if we return `[false, false, true, false]`,
68
68
  Array will become `[false, false, Element, true, false]`.
69
69
 
70
+ ---
71
+ ${\textsf{\color{CornflowerBlue}function}}$ removeFromArray - Delete value from array.
72
+ Only deletes first encountered.
73
+
74
+ Returns index of deleted value.
75
+
76
+ ---
77
+ ${\textsf{\color{CornflowerBlue}function}}$ removeLastFromArray - Delete value from array.
78
+ Only deletes last encountered.
79
+
80
+ Returns index of deleted value.
81
+
70
82
  ---
71
83
 
72
84
 
package/dist/arrays.d.ts CHANGED
@@ -29,3 +29,17 @@ export declare function permutations<T>(array: T[]): T[][];
29
29
  * Array will become `[false, false, Element, true, false]`.
30
30
  */
31
31
  export declare function pushToSorted<T>(array: T[], element: T, compare: (element: T) => boolean): void;
32
+ /**
33
+ * Delete value from array.
34
+ * Only deletes first encountered.
35
+ *
36
+ * Returns index of deleted value.
37
+ */
38
+ export declare function removeFromArray<T>(array: T[], value: T): number;
39
+ /**
40
+ * Delete value from array.
41
+ * Only deletes last encountered.
42
+ *
43
+ * Returns index of deleted value.
44
+ */
45
+ export declare function removeLastFromArray<T>(array: T[], value: T): number;
package/dist/arrays.js CHANGED
@@ -104,3 +104,27 @@ export function pushToSorted(array, element, compare) {
104
104
  const index = array.findIndex(compare);
105
105
  array.splice(index === -1 ? array.length : index, 0, element);
106
106
  }
107
+ /**
108
+ * Delete value from array.
109
+ * Only deletes first encountered.
110
+ *
111
+ * Returns index of deleted value.
112
+ */
113
+ export function removeFromArray(array, value) {
114
+ const index = array.indexOf(value);
115
+ if (index !== -1)
116
+ array.splice(index, 1);
117
+ return index;
118
+ }
119
+ /**
120
+ * Delete value from array.
121
+ * Only deletes last encountered.
122
+ *
123
+ * Returns index of deleted value.
124
+ */
125
+ export function removeLastFromArray(array, value) {
126
+ const index = array.lastIndexOf(value);
127
+ if (index !== -1)
128
+ array.splice(index, 1);
129
+ return index;
130
+ }
package/dist/objects.d.ts CHANGED
@@ -45,10 +45,12 @@ export declare function pick<T extends object, K extends keyof T>(object: T, key
45
45
  * ```
46
46
  */
47
47
  export declare class Base {
48
- id: number;
49
48
  static lastId: number;
50
49
  static idMap: Map<number, Base>;
51
50
  static subclasses: Map<string, Constructor<Base>>;
51
+ private _id;
52
+ get id(): number;
53
+ set id(value: number);
52
54
  constructor(id?: number);
53
55
  static registerSubclass(): void;
54
56
  }
package/dist/objects.js CHANGED
@@ -118,12 +118,20 @@ export function pick(object, keys) {
118
118
  * ```
119
119
  */
120
120
  export class Base {
121
- id;
122
121
  static lastId = 0;
123
122
  static idMap = new Map();
124
123
  static subclasses = new Map();
124
+ _id;
125
+ get id() {
126
+ return this._id;
127
+ }
128
+ set id(value) {
129
+ Base.idMap.delete(this._id);
130
+ Base.idMap.set(value, this);
131
+ this._id = value;
132
+ }
125
133
  constructor(id = ++Base.lastId) {
126
- this.id = id;
134
+ this._id = id;
127
135
  Base.idMap.set(id, this);
128
136
  }
129
137
  static registerSubclass() {
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@softsky/utils",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "description": "JavaScript/TypeScript utilities",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "lint": "eslint \"./src/**/*.ts\" --fix && tsc",
8
8
  "gen-readme": "bun ./generate-readme.ts",
9
- "prepublishOnly": "bun run test && bun run lint && bun run gen-readme",
9
+ "prepublishOnly": "npm i && bun run test && bun run lint && bun run gen-readme",
10
10
  "test": "bun test"
11
11
  },
12
12
  "repository": {