flat-pair 1.1.0 → 1.3.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.
package/README.md CHANGED
@@ -14,7 +14,7 @@ For more awesome packages, check out [my homepage💛](https://baendlorel.github
14
14
  - 🛡️ **Type Safe**: Full TypeScript support with generic types
15
15
  - 🎯 **Zero Cost**: Static methods available for minimal overhead
16
16
  - ⚡ **Lightweight**: No dependencies, minimal bundle size
17
- - 🌲 **Native Behavior**: `forEach`, `find` acts like they are in `Array`. Equality check uses `Object.is`
17
+ - 🌲 **Native Behavior**: `forEach`, `find` acts like they are in `Array`. Equality check uses `SameValueZero`
18
18
 
19
19
  ## Installation / Import
20
20
 
@@ -37,8 +37,8 @@ import { FlatPair, FlatPairOperator, add, get, find ...others } from 'flat-pair'
37
37
  ```typescript
38
38
  // userId - detail pair
39
39
  const pairs = new FlatPair<number, Detail>();
40
- pairs.add(1, { name: 'Alice', age: 30 });
41
- pairs.add(2, { name: 'Bob', age: 25 });
40
+ pairs.add(1, { name: 'Alice', age: 30 }); // won't change value when key exists
41
+ pairs.set(2, { name: 'Bob', age: 25 });
42
42
  pairs.remove(1);
43
43
  pairs.find((value, key) => value.age > 20);
44
44
  ```
package/dist/index.d.ts CHANGED
@@ -10,8 +10,13 @@ declare class FlatPair<K, V> {
10
10
  get array(): any[];
11
11
  /**
12
12
  * Will check if the key already exists, if so then do nothing.
13
+ * - different from Map.set, **won't** change value if key exists
13
14
  */
14
15
  add(key: K, value: V): this;
16
+ /**
17
+ * Same as Map.set
18
+ */
19
+ set(key: K, value: V): this;
15
20
  hasByValue(value: V): boolean;
16
21
  remove(key: K): boolean;
17
22
  removeByValue(value: V): boolean;
@@ -32,7 +37,15 @@ declare class FlatPair<K, V> {
32
37
  * Creates an static operator
33
38
  */
34
39
  declare class FlatPairOperator<K, V> {
40
+ /**
41
+ * Will check if the key already exists, if so then do nothing.
42
+ * - different from Map.set, **won't** change value if key exists
43
+ */
35
44
  add(items: any[], key: K, value: V): void;
45
+ /**
46
+ * Same as Map.set
47
+ */
48
+ set(items: any[], key: K, value: V): void;
36
49
  remove(items: any[], key: K): boolean;
37
50
  get(items: any[], key: K): V | undefined;
38
51
  getByValue(items: any[], value: V): K | undefined;
@@ -49,7 +62,15 @@ declare class FlatPairOperator<K, V> {
49
62
  }
50
63
 
51
64
  declare function size(items: any[]): number;
65
+ /**
66
+ * Will check if the key already exists, if so then do nothing.
67
+ * - different from Map.set, **won't** change value if key exists
68
+ */
52
69
  declare function add<K, V>(items: any[], key: K, value: V): void;
70
+ /**
71
+ * Same as Map.set
72
+ */
73
+ declare function set<K, V>(items: any[], key: K, value: V): void;
53
74
  declare function has<K>(items: any[], key: K): boolean;
54
75
  declare function hasByValue<V>(items: any[], value: V): boolean;
55
76
  declare function get<K, V>(items: any[], key: K): V | undefined;
@@ -66,4 +87,4 @@ declare function clear(items: any[]): void;
66
87
  declare function find<K, V>(items: any[], predicate: (value: V, key: K, index: number, array: any[]) => boolean): [K, V] | undefined;
67
88
  declare function findIndex<K, V>(items: any[], predicate: (value: V, key: K, index: number, array: any[]) => boolean): number;
68
89
 
69
- export { FlatPair, FlatPairOperator, add, clear, find, findIndex, forEach, get, getByValue, has, hasByValue, remove, removeByValue, size };
90
+ export { FlatPair, FlatPairOperator, add, clear, find, findIndex, forEach, get, getByValue, has, hasByValue, remove, removeByValue, set, size };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- const t=Object.is;function r(t){const r=t.length;if(r%2!=0)throw new Error("Invalid items length, must be even number");return r/2}function e(r,e,n){const i=r.length;for(let n=0;n<i;n+=2)if(t(r[n],e))return;r.push(e,n)}function n(r,e){const n=r.length;for(let i=0;i<n;i+=2)if(t(r[i],e))return!0;return!1}function i(r,e){const n=r.length;for(let i=1;i<n;i+=2)if(t(r[i],e))return!0;return!1}function u(r,e){const n=r.length;for(let i=0;i<n;i+=2)if(t(r[i],e))return r[i+1]}function o(r,e){const n=r.length;for(let i=1;i<n;i+=2)if(t(r[i],e))return r[i-1]}function s(r,e){const n=r.length;for(let i=0;i<n;i+=2)if(t(r[i],e))return r.splice(i,2),!0;return!1}function f(r,e){const n=r.length;for(let i=1;i<n;i+=2)if(t(r[i],e))return r.splice(i-1,2),!0;return!1}function l(t,r,e){const n=t.length;for(let i=0;i<n;i+=2)i in t&&r.call(e,t[i+1],t[i],i,t)}function c(t){t.length=0}function h(t,r){const e=t.length;for(let n=0;n<e;n++){if(r(t[n+1],t[n],n,t))return[t[n],t[n+1]]}}function a(t,r){const e=t.length;for(let n=0;n<e;n++){if(r(t[n+1],t[n],n,t))return n}return-1}class y{static from(t){if("[object Map]"!==Object.prototype.toString.call(t))throw new TypeError("[FlatPair: __func__] Argument must be a Map");const r=[];return t.forEach((t,e)=>r.push(e,t)),new y(r)}t=[];constructor(t){if(t.length%2!=0)throw new TypeError("FlatPair items length must be even");this.t=t}get size(){return this.t.length/2}get array(){return this.t}add(t,r){return e(this.t,t,r),this}hasByValue(t){return i(this.t,t)}remove(t){return s(this.t,t)}removeByValue(t){return f(this.t,t)}get(t){return u(this.t,t)}getByValue(t){return o(this.t,t)}find(t){return h(this.t,t)}findIndex(t){return a(this.t,t)}clear(){this.t.length=0}has(t){return n(this.t,t)}forEach(t,r){l(this.t,t,r)}*keys(){for(let t=0;t<this.t.length;t+=2)yield this.t[t]}*values(){for(let t=1;t<this.t.length;t+=2)yield this.t[t]}*entries(){for(let t=0;t<this.t.length;t+=2)yield[this.t[t],this.t[t+1]]}[Symbol.iterator](){return this.entries()}}class d{add(t,r,n){e(t,r,n)}remove(t,r){return s(t,r)}get(t,r){return u(t,r)}getByValue(t,r){return o(t,r)}find(t,r){return h(t,r)}findIndex(t,r){return a(t,r)}removeByValue(t,r){return f(t,r)}forEach(t,r,e){l(t,r,e)}*keys(t){for(let r=0;r<t.length;r+=2)yield t[r]}*values(t){for(let r=1;r<t.length;r+=2)yield t[r]}*entries(t){for(let r=0;r<t.length;r+=2)yield[t[r],t[r+1]]}clear(t){t.length=0}has(t,r){return n(t,r)}hasByValue(t,r){return i(t,r)}}export{y as FlatPair,d as FlatPairOperator,e as add,c as clear,h as find,a as findIndex,l as forEach,u as get,o as getByValue,n as has,i as hasByValue,s as remove,f as removeByValue,r as size};
1
+ const e=function(e,r){return e===r||e!=e&&r!=r};function r(e){const r=e.length;if(r%2!=0)throw new TypeError("[FlatPair: size] items length must be even number");return r/2}function t(r,t,n){const i=r.length;if(i%2!=0)throw new TypeError("[FlatPair: add] items length must be even number");for(let n=0;n<i;n+=2)if(e(r[n],t))return;r.push(t,n)}function n(r,t,n){const i=r.length;if(i%2!=0)throw new TypeError("[FlatPair: set] items length must be even number");for(let u=0;u<i;u+=2)if(e(r[u],t))return void(r[u+1]=n);r.push(t,n)}function i(r,t){const n=r.length;if(n%2!=0)throw new TypeError("[FlatPair: has] items length must be even number");for(let i=0;i<n;i+=2)if(e(r[i],t))return!0;return!1}function u(r,t){const n=r.length;if(n%2!=0)throw new TypeError("[FlatPair: hasByValue] items length must be even number");for(let i=1;i<n;i+=2)if(e(r[i],t))return!0;return!1}function o(r,t){const n=r.length;if(n%2!=0)throw new TypeError("[FlatPair: get] items length must be even number");for(let i=0;i<n;i+=2)if(e(r[i],t))return r[i+1]}function s(r,t){const n=r.length;if(n%2!=0)throw new TypeError("[FlatPair: getByValue] items length must be even number");for(let i=1;i<n;i+=2)if(e(r[i],t))return r[i-1]}function f(r,t){const n=r.length;if(n%2!=0)throw new TypeError("[FlatPair: remove] items length must be even number");for(let i=0;i<n;i+=2)if(e(r[i],t))return r.splice(i,2),!0;return!1}function l(r,t){const n=r.length;if(n%2!=0)throw new TypeError("[FlatPair: removeByValue] items length must be even number");for(let i=1;i<n;i+=2)if(e(r[i],t))return r.splice(i-1,2),!0;return!1}function a(e,r,t){const n=e.length;if(n%2!=0)throw new TypeError("[FlatPair: forEach] items length must be even number");for(let i=0;i<n;i+=2)i in e&&r.call(t,e[i+1],e[i],i,e)}function h(e){e.length=0}function m(e,r){const t=e.length;if(t%2!=0)throw new TypeError("[FlatPair: find] items length must be even number");for(let n=0;n<t;n+=2){if(r(e[n+1],e[n],n,e))return[e[n],e[n+1]]}}function c(e,r){const t=e.length;if(t%2!=0)throw new TypeError("[FlatPair: findIndex] items length must be even number");for(let n=0;n<t;n+=2){if(r(e[n+1],e[n],n,e))return n}return-1}class y{static from(e){if("[object Map]"!==Object.prototype.toString.call(e))throw new TypeError("[FlatPair: from] Argument must be a Map");const r=[];return e.forEach((e,t)=>r.push(t,e)),new y(r)}t=[];constructor(e){if(e.length%2!=0)throw new TypeError("[FlatPair: constructor] items length must be even number");this.t=e.slice()}get size(){return this.t.length/2}get array(){return this.t}add(e,r){return t(this.t,e,r),this}set(e,r){return n(this.t,e,r),this}hasByValue(e){return u(this.t,e)}remove(e){return f(this.t,e)}removeByValue(e){return l(this.t,e)}get(e){return o(this.t,e)}getByValue(e){return s(this.t,e)}find(e){return m(this.t,e)}findIndex(e){return c(this.t,e)}clear(){this.t.length=0}has(e){return i(this.t,e)}forEach(e,r){a(this.t,e,r)}*keys(){for(let e=0;e<this.t.length;e+=2)yield this.t[e]}*values(){for(let e=1;e<this.t.length;e+=2)yield this.t[e]}*entries(){for(let e=0;e<this.t.length;e+=2)yield[this.t[e],this.t[e+1]]}[Symbol.iterator](){return this.entries()}}class b{add(e,r,n){t(e,r,n)}set(e,r,t){n(e,r,t)}remove(e,r){return f(e,r)}get(e,r){return o(e,r)}getByValue(e,r){return s(e,r)}find(e,r){return m(e,r)}findIndex(e,r){return c(e,r)}removeByValue(e,r){return l(e,r)}forEach(e,r,t){a(e,r,t)}*keys(e){for(let r=0;r<e.length;r+=2)yield e[r]}*values(e){for(let r=1;r<e.length;r+=2)yield e[r]}*entries(e){for(let r=0;r<e.length;r+=2)yield[e[r],e[r+1]]}clear(e){e.length=0}has(e,r){return i(e,r)}hasByValue(e,r){return u(e,r)}}export{y as FlatPair,b as FlatPairOperator,t as add,h as clear,m as find,c as findIndex,a as forEach,o as get,s as getByValue,i as has,u as hasByValue,f as remove,l as removeByValue,n as set,r as size};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flat-pair",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "author": {
5
5
  "name": "Kasukabe Tsumugi",
6
6
  "email": "futami16237@gmail.com"