value-enhancer
Advanced tools
Comparing version 4.1.1 to 4.1.2
@@ -22,3 +22,3 @@ import { R as ReadonlyVal } from './typings-dd45eb69.js'; | ||
#private; | ||
constructor(arrayLike?: ArrayLike<TValue>); | ||
constructor(items?: Iterable<TValue> | null); | ||
/** | ||
@@ -99,9 +99,35 @@ * A readonly val with value of the internal readonly array. | ||
/** | ||
* Sets new element to the list at specific index in place of the existing element. | ||
* @param index The zero-based location in the list at which to insert element. | ||
* Same as `Array.prototype.splice`. | ||
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. | ||
* @param start The zero-based location in the array from which to start removing elements. | ||
* A negative index will be ignored. | ||
* @param item Element to insert into the list. | ||
* @param deleteCount The number of elements to remove. | ||
* @returns An array containing the elements that were deleted. | ||
*/ | ||
set(index: number, item: TValue): void; | ||
splice(start: number, deleteCount?: number): TValue[]; | ||
/** | ||
* Same as `Array.prototype.splice`. | ||
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. | ||
* @param start The zero-based location in the array from which to start removing elements. | ||
* A negative index will be ignored. | ||
* @param deleteCount The number of elements to remove. | ||
* @param items Elements to insert into the array in place of the deleted elements. | ||
* @returns An array containing the elements that were deleted. | ||
*/ | ||
splice(start: number, deleteCount: number, ...items: TValue[]): TValue[]; | ||
/** | ||
* Sets new item to the list at specific index in place of the existing item. | ||
* @param index The zero-based location in the list at which to insert item. | ||
* A negative index will be ignored. | ||
* @param item Item to set to the list. | ||
* @returns this | ||
*/ | ||
set(index: number, item: TValue): this; | ||
/** | ||
* Sets new items to the list at specific index in place of the existing items. | ||
* @param entries An iterable object that contains key-value pairs. | ||
* @returns this | ||
*/ | ||
batchSet(entries: Iterable<readonly [number, TValue]>): this; | ||
/** | ||
* Inserts new elements to the list. Pushes existing elements to the right. | ||
@@ -164,3 +190,3 @@ * @param index The zero-based location in the list from which to start inserting elements. | ||
*/ | ||
type ReadonlyReactiveList<TValue> = Omit<ReactiveList<TValue>, "$" | "length" | "push" | "pop" | "pushHead" | "popHead" | "set" | "insert" | "delete" | "clear" | "replace" | "reverse" | "sort"> & { | ||
type ReadonlyReactiveList<TValue> = Omit<ReactiveList<TValue>, "length" | "push" | "pop" | "pushHead" | "popHead" | "set" | "insert" | "delete" | "clear" | "replace" | "reverse" | "sort"> & { | ||
/** | ||
@@ -194,3 +220,3 @@ * Gets the length of the array. This is a number one higher than the highest index in the array. | ||
#private; | ||
constructor(entries?: readonly (readonly [TKey, TValue])[] | null); | ||
constructor(entries?: Iterable<readonly [TKey, TValue]> | null); | ||
/** | ||
@@ -203,5 +229,13 @@ * A readonly val with value of `this`. | ||
delete(key: TKey): boolean; | ||
/** | ||
* Delete multiple entries from the Map. | ||
*/ | ||
batchDelete(keys: Iterable<TKey>): boolean; | ||
clear(): void; | ||
set(key: TKey, value: TValue): this; | ||
/** | ||
* Set multiple entries in the Map. | ||
*/ | ||
batchSet(entries: Iterable<readonly [TKey, TValue]>): this; | ||
/** | ||
* Replace all entries in the Map. | ||
@@ -218,4 +252,4 @@ * | ||
*/ | ||
type ReadonlyReactiveMap<TKey, TValue> = Omit<ReactiveMap<TKey, TValue>, "$" | "delete" | "clear" | "set" | "replace"> & { | ||
readonly: ReadonlyVal<ReadonlyReactiveMap<TKey, TValue>>; | ||
type ReadonlyReactiveMap<TKey, TValue> = Omit<ReactiveMap<TKey, TValue>, "$" | "delete" | "clear" | "set" | "batchSet" | "replace"> & { | ||
readonly $: ReadonlyVal<ReadonlyReactiveMap<TKey, TValue>>; | ||
}; | ||
@@ -242,3 +276,3 @@ | ||
#private; | ||
constructor(entries?: readonly TValue[] | null); | ||
constructor(values?: Iterable<TValue> | null); | ||
/** | ||
@@ -250,5 +284,13 @@ * A readonly val with value of `this`. | ||
readonly $: ReadonlyVal<this>; | ||
delete(key: TValue): boolean; | ||
delete(value: TValue): boolean; | ||
/** | ||
* Delete multiple values from the Set. | ||
*/ | ||
batchDelete(values: Iterable<TValue>): boolean; | ||
clear(): void; | ||
add(value: TValue): this; | ||
/** | ||
* Add multiple values to the Set. | ||
*/ | ||
batchAdd(values: Iterable<TValue>): this; | ||
dispose(): void; | ||
@@ -255,0 +297,0 @@ /** |
@@ -222,5 +222,5 @@ 'use strict'; | ||
var ReactiveList = class { | ||
constructor(arrayLike) { | ||
constructor(items) { | ||
const [$, set$] = readonlyVal( | ||
arrayLike ? Array.from(arrayLike) : [], | ||
items ? [...items] : [], | ||
{ equal: false } | ||
@@ -354,15 +354,46 @@ ); | ||
} | ||
splice(start, deleteCount, ...rest) { | ||
const result = this.array.splice( | ||
start, | ||
deleteCount, | ||
...rest | ||
); | ||
if (result.length > 0 || rest.length > 0) { | ||
this.#notify(); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Sets new element to the list at specific index in place of the existing element. | ||
* @param index The zero-based location in the list at which to insert element. | ||
* Sets new item to the list at specific index in place of the existing item. | ||
* @param index The zero-based location in the list at which to insert item. | ||
* A negative index will be ignored. | ||
* @param item Element to insert into the list. | ||
* @param item Item to set to the list. | ||
* @returns this | ||
*/ | ||
set(index, item) { | ||
if (index >= 0) { | ||
if (index >= 0 && this.array[index] !== item) { | ||
this.array[index] = item; | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Sets new items to the list at specific index in place of the existing items. | ||
* @param entries An iterable object that contains key-value pairs. | ||
* @returns this | ||
*/ | ||
batchSet(entries) { | ||
let isDirty = false; | ||
for (const [index, item] of entries) { | ||
if (index >= 0 && this.array[index] !== item) { | ||
isDirty = true; | ||
this.array[index] = item; | ||
} | ||
} | ||
if (isDirty) { | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Inserts new elements to the list. Pushes existing elements to the right. | ||
@@ -375,4 +406,3 @@ * @param index The zero-based location in the list from which to start inserting elements. | ||
if (index >= 0 && items.length > 0) { | ||
this.array.splice(index, 0, ...items); | ||
this.#notify(); | ||
this.splice(index, 0, ...items); | ||
} | ||
@@ -389,6 +419,3 @@ } | ||
if (index >= 0 && count >= 1) { | ||
const result = this.array.splice(index, count); | ||
if (result.length > 0) { | ||
this.#notify(); | ||
} | ||
this.splice(index, count); | ||
} | ||
@@ -501,2 +528,15 @@ } | ||
} | ||
/** | ||
* Delete multiple entries from the Map. | ||
*/ | ||
batchDelete(keys) { | ||
let deleted = false; | ||
for (const key of keys) { | ||
deleted = super.delete(key) || deleted; | ||
} | ||
if (deleted) { | ||
this.#notify(); | ||
} | ||
return deleted; | ||
} | ||
clear() { | ||
@@ -517,2 +557,16 @@ if (this.size > 0) { | ||
/** | ||
* Set multiple entries in the Map. | ||
*/ | ||
batchSet(entries) { | ||
let isDirty = false; | ||
for (const [key, value] of entries) { | ||
isDirty = isDirty || !this.has(key) || this.get(key) !== value; | ||
super.set(key, value); | ||
} | ||
if (isDirty) { | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Replace all entries in the Map. | ||
@@ -543,3 +597,3 @@ * | ||
var ReactiveSet = class extends Set { | ||
constructor(entries) { | ||
constructor(values) { | ||
super(); | ||
@@ -549,6 +603,4 @@ const [$, set$] = readonlyVal(this, { equal: false }); | ||
this.#notify = () => set$(this); | ||
if (entries) { | ||
for (const value of entries) { | ||
this.add(value); | ||
} | ||
if (values) { | ||
this.batchAdd(values); | ||
} | ||
@@ -563,4 +615,4 @@ } | ||
#notify; | ||
delete(key) { | ||
const deleted = super.delete(key); | ||
delete(value) { | ||
const deleted = super.delete(value); | ||
if (deleted) { | ||
@@ -571,2 +623,15 @@ this.#notify(); | ||
} | ||
/** | ||
* Delete multiple values from the Set. | ||
*/ | ||
batchDelete(values) { | ||
let deleted = false; | ||
for (const value of values) { | ||
deleted = super.delete(value) || deleted; | ||
} | ||
if (deleted) { | ||
this.#notify(); | ||
} | ||
return deleted; | ||
} | ||
clear() { | ||
@@ -586,2 +651,15 @@ if (this.size > 0) { | ||
} | ||
/** | ||
* Add multiple values to the Set. | ||
*/ | ||
batchAdd(values) { | ||
const prevSize = this.size; | ||
for (const value of values) { | ||
super.add(value); | ||
} | ||
if (prevSize !== this.size) { | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
dispose() { | ||
@@ -588,0 +666,0 @@ this.$.dispose(); |
{ | ||
"name": "value-enhancer", | ||
"version": "4.1.1", | ||
"version": "4.1.2", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A tiny library to enhance value with reactive wrapper.", |
@@ -453,9 +453,9 @@ # [value-enhancer](https://github.com/crimx/value-enhancer) | ||
map.set("someKey", v); | ||
map.set("someKey", v); // set a val, the value inside the val is subscribed and flatten to `item$` | ||
console.log(item$.value); // "someValue" | ||
v.set("someValue2"); | ||
v.set("someValue2"); // you can also set a non-val value, which is passed to `item$`` directly | ||
console.log(item$.value); // "someValue2" | ||
``` |
@@ -22,5 +22,5 @@ import { readonlyVal } from "../readonly-val"; | ||
export class ReactiveList<TValue> { | ||
public constructor(arrayLike?: ArrayLike<TValue>) { | ||
public constructor(items?: Iterable<TValue> | null) { | ||
const [$, set$] = readonlyVal<ReadonlyArray<TValue>>( | ||
arrayLike ? Array.from(arrayLike) : [], | ||
items ? [...items] : [], | ||
{ equal: false } | ||
@@ -172,15 +172,75 @@ ); | ||
/** | ||
* Sets new element to the list at specific index in place of the existing element. | ||
* @param index The zero-based location in the list at which to insert element. | ||
* Same as `Array.prototype.splice`. | ||
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. | ||
* @param start The zero-based location in the array from which to start removing elements. | ||
* A negative index will be ignored. | ||
* @param item Element to insert into the list. | ||
* @param deleteCount The number of elements to remove. | ||
* @returns An array containing the elements that were deleted. | ||
*/ | ||
public set(index: number, item: TValue): void { | ||
if (index >= 0) { | ||
public splice(start: number, deleteCount?: number): TValue[]; | ||
/** | ||
* Same as `Array.prototype.splice`. | ||
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. | ||
* @param start The zero-based location in the array from which to start removing elements. | ||
* A negative index will be ignored. | ||
* @param deleteCount The number of elements to remove. | ||
* @param items Elements to insert into the array in place of the deleted elements. | ||
* @returns An array containing the elements that were deleted. | ||
*/ | ||
public splice( | ||
start: number, | ||
deleteCount: number, | ||
...items: TValue[] | ||
): TValue[]; | ||
public splice( | ||
start: number, | ||
deleteCount?: number, | ||
...rest: TValue[] | ||
): TValue[] { | ||
const result = (this.array as TValue[]).splice( | ||
start as number, | ||
deleteCount as number, | ||
...(rest as TValue[]) | ||
); | ||
if (result.length > 0 || rest.length > 0) { | ||
this.#notify(); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Sets new item to the list at specific index in place of the existing item. | ||
* @param index The zero-based location in the list at which to insert item. | ||
* A negative index will be ignored. | ||
* @param item Item to set to the list. | ||
* @returns this | ||
*/ | ||
public set(index: number, item: TValue): this { | ||
if (index >= 0 && this.array[index] !== item) { | ||
(this.array as TValue[])[index] = item; | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Sets new items to the list at specific index in place of the existing items. | ||
* @param entries An iterable object that contains key-value pairs. | ||
* @returns this | ||
*/ | ||
public batchSet(entries: Iterable<readonly [number, TValue]>): this { | ||
let isDirty = false; | ||
for (const [index, item] of entries) { | ||
if (index >= 0 && this.array[index] !== item) { | ||
isDirty = true; | ||
(this.array as TValue[])[index] = item; | ||
} | ||
} | ||
if (isDirty) { | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Inserts new elements to the list. Pushes existing elements to the right. | ||
@@ -193,4 +253,3 @@ * @param index The zero-based location in the list from which to start inserting elements. | ||
if (index >= 0 && items.length > 0) { | ||
(this.array as TValue[]).splice(index, 0, ...items); | ||
this.#notify(); | ||
this.splice(index, 0, ...items); | ||
} | ||
@@ -208,6 +267,3 @@ } | ||
if (index >= 0 && count >= 1) { | ||
const result = (this.array as TValue[]).splice(index, count); | ||
if (result.length > 0) { | ||
this.#notify(); | ||
} | ||
this.splice(index, count); | ||
} | ||
@@ -307,3 +363,2 @@ } | ||
ReactiveList<TValue>, | ||
| "$" | ||
| "length" | ||
@@ -310,0 +365,0 @@ | "push" |
@@ -25,3 +25,3 @@ import { readonlyVal } from "../readonly-val"; | ||
export class ReactiveMap<TKey, TValue> extends Map<TKey, TValue> { | ||
public constructor(entries?: readonly (readonly [TKey, TValue])[] | null) { | ||
public constructor(entries?: Iterable<readonly [TKey, TValue]> | null) { | ||
super(); | ||
@@ -57,2 +57,16 @@ | ||
/** | ||
* Delete multiple entries from the Map. | ||
*/ | ||
public batchDelete(keys: Iterable<TKey>): boolean { | ||
let deleted = false; | ||
for (const key of keys) { | ||
deleted = super.delete(key) || deleted; | ||
} | ||
if (deleted) { | ||
this.#notify(); | ||
} | ||
return deleted; | ||
} | ||
public override clear(): void { | ||
@@ -75,2 +89,17 @@ if (this.size > 0) { | ||
/** | ||
* Set multiple entries in the Map. | ||
*/ | ||
public batchSet(entries: Iterable<readonly [TKey, TValue]>): this { | ||
let isDirty = false; | ||
for (const [key, value] of entries) { | ||
isDirty = isDirty || !this.has(key) || this.get(key) !== value; | ||
super.set(key, value); | ||
} | ||
if (isDirty) { | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Replace all entries in the Map. | ||
@@ -108,5 +137,5 @@ * | ||
ReactiveMap<TKey, TValue>, | ||
"$" | "delete" | "clear" | "set" | "replace" | ||
"$" | "delete" | "clear" | "set" | "batchSet" | "replace" | ||
> & { | ||
readonly: ReadonlyVal<ReadonlyReactiveMap<TKey, TValue>>; | ||
readonly $: ReadonlyVal<ReadonlyReactiveMap<TKey, TValue>>; | ||
}; |
@@ -22,3 +22,3 @@ import { readonlyVal } from "../readonly-val"; | ||
export class ReactiveSet<TValue> extends Set<TValue> { | ||
public constructor(entries?: readonly TValue[] | null) { | ||
public constructor(values?: Iterable<TValue> | null) { | ||
super(); | ||
@@ -30,6 +30,4 @@ | ||
if (entries) { | ||
for (const value of entries) { | ||
this.add(value); | ||
} | ||
if (values) { | ||
this.batchAdd(values); | ||
} | ||
@@ -47,4 +45,4 @@ } | ||
public override delete(key: TValue): boolean { | ||
const deleted = super.delete(key); | ||
public override delete(value: TValue): boolean { | ||
const deleted = super.delete(value); | ||
if (deleted) { | ||
@@ -56,2 +54,16 @@ this.#notify(); | ||
/** | ||
* Delete multiple values from the Set. | ||
*/ | ||
public batchDelete(values: Iterable<TValue>): boolean { | ||
let deleted = false; | ||
for (const value of values) { | ||
deleted = super.delete(value) || deleted; | ||
} | ||
if (deleted) { | ||
this.#notify(); | ||
} | ||
return deleted; | ||
} | ||
public override clear(): void { | ||
@@ -73,2 +85,16 @@ if (this.size > 0) { | ||
/** | ||
* Add multiple values to the Set. | ||
*/ | ||
public batchAdd(values: Iterable<TValue>): this { | ||
const prevSize = this.size; | ||
for (const value of values) { | ||
super.add(value); | ||
} | ||
if (prevSize !== this.size) { | ||
this.#notify(); | ||
} | ||
return this; | ||
} | ||
public dispose(): void { | ||
@@ -75,0 +101,0 @@ this.$.dispose(); |
Sorry, the diff of this file is not supported yet
141602
4259