space-lift
Advanced tools
Comparing version 1.0.0-beta.8 to 1.0.0-beta.9
@@ -22,5 +22,5 @@ import { Draft } from './immupdate'; | ||
/** | ||
* Appends an Array of items at the end of the Array. | ||
* Appends an Iterable of items at the end of the Array. | ||
*/ | ||
appendAll(items: ReadonlyArray<T[number]>): ArrayWrapper<T>; | ||
appendAll(items: Iterable<T[number]>): ArrayWrapper<T>; | ||
/** | ||
@@ -27,0 +27,0 @@ * Filters all the falsy elements out of this Array. |
@@ -37,6 +37,6 @@ "use strict"; | ||
/** | ||
* Appends an Array of items at the end of the Array. | ||
* Appends an Iterable of items at the end of the Array. | ||
*/ | ||
appendAll(items) { | ||
return new ArrayWrapper(this._value.concat(items)); | ||
return new ArrayWrapper([...this._value, ...items]); | ||
} | ||
@@ -43,0 +43,0 @@ /** |
@@ -50,10 +50,2 @@ /** | ||
/** | ||
* Efficiently inserts a string|number item in an already sorted Array. | ||
*/ | ||
insertSorted<U extends string | number>(this: DraftArray<U>, item: U): void; | ||
/** | ||
* Efficiently inserts an item in an already sorted Array. The second argument is the key on which items are sorted. | ||
*/ | ||
insertSorted(item: T, bySortKey: (item: T) => string | number): void; | ||
/** | ||
* Runs a predicate for each item of the Array. | ||
@@ -60,0 +52,0 @@ * If it returns true, a Draft item is created and given to updateFunction, ready to be mutated. |
@@ -73,19 +73,2 @@ "use strict"; | ||
insert: (item, index) => mutateArray(() => getArray().splice(index, 0, item), allKeys), | ||
insertSorted: (item, by = item => item) => mutateArray(() => { | ||
const arr = getArray(); | ||
let low = 0, high = arr.length; | ||
const itemValue = by(item); | ||
while (low < high) { | ||
const mid = (low + high) >>> 1; | ||
const midValue = by(arr[mid]); | ||
const itemValueIsBigger = typeof itemValue === 'string' | ||
? itemValue.localeCompare(midValue) > 0 | ||
: itemValue > midValue; | ||
if (itemValueIsBigger) | ||
low = mid + 1; | ||
else | ||
high = mid; | ||
} | ||
arr.splice(low, 0, item); | ||
}, allKeys), | ||
updateIf: (predicate, updateFunction) => getArray().forEach((item, index) => { | ||
@@ -92,0 +75,0 @@ if (predicate(item, index)) |
export { lift } from './lift'; | ||
export { update } from './immupdate'; | ||
export { range } from './array'; | ||
export { createUnion } from './union'; | ||
export { createUnion, empty } from './union'; | ||
export { createEnum } from './enum'; | ||
@@ -6,0 +6,0 @@ export { identity, noop } from './function'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.immutable = exports.is = exports.noop = exports.identity = exports.createEnum = exports.createUnion = exports.range = exports.update = exports.lift = void 0; | ||
exports.immutable = exports.is = exports.noop = exports.identity = exports.createEnum = exports.empty = exports.createUnion = exports.range = exports.update = exports.lift = void 0; | ||
const lift_1 = require("./lift"); | ||
@@ -13,2 +13,3 @@ var lift_2 = require("./lift"); | ||
Object.defineProperty(exports, "createUnion", { enumerable: true, get: function () { return union_1.createUnion; } }); | ||
Object.defineProperty(exports, "empty", { enumerable: true, get: function () { return union_1.empty; } }); | ||
var enum_1 = require("./enum"); | ||
@@ -15,0 +16,0 @@ Object.defineProperty(exports, "createEnum", { enumerable: true, get: function () { return enum_1.createEnum; } }); |
@@ -0,1 +1,2 @@ | ||
import { ObjectWrapper } from './object'; | ||
import { Draft } from './immupdate'; | ||
@@ -45,2 +46,3 @@ import type { Pipe } from './lift'; | ||
toArray(): import("./array").ArrayWrapper<[K, V][]>; | ||
toObject<KK extends string | number>(this: MapWrapper<KK, V, any>): ObjectWrapper<Record<KK, V | undefined>>; | ||
} | ||
@@ -47,0 +49,0 @@ export declare function setMapPipe(_pipe: Pipe): void; |
@@ -73,2 +73,8 @@ "use strict"; | ||
} | ||
toObject() { | ||
return this.pipe(m => [...m].reduce((obj, [key, val]) => { | ||
obj[key] = val; | ||
return obj; | ||
}, {})); | ||
} | ||
} | ||
@@ -75,0 +81,0 @@ exports.MapWrapper = MapWrapper; |
import { ArrayWrapper } from './array'; | ||
import { MapWrapper } from './map'; | ||
import { Draft } from './immupdate'; | ||
@@ -41,3 +42,9 @@ import type { Pipe } from './lift'; | ||
update(updateFunction: (draft: Draft<T>) => void): import("./lift").Lifted<T>; | ||
/** | ||
* Transforms this Object to a Map where the keys are the string typed keys of this Object. | ||
*/ | ||
toMap(): MapWrapper<KeyAsString<keyof T>, T[keyof T], Map<KeyAsString<keyof T>, T[keyof T]>>; | ||
} | ||
export declare function setObjectPipe(_pipe: Pipe): void; | ||
declare type KeyAsString<K> = K extends string ? K : string; | ||
export {}; |
@@ -63,2 +63,8 @@ "use strict"; | ||
} | ||
/** | ||
* Transforms this Object to a Map where the keys are the string typed keys of this Object. | ||
*/ | ||
toMap() { | ||
return this.pipe(o => new Map(Object.entries(o))); | ||
} | ||
} | ||
@@ -65,0 +71,0 @@ exports.ObjectWrapper = ObjectWrapper; |
@@ -11,2 +11,3 @@ import type { Pipe } from './lift'; | ||
add(item: T): this; | ||
addAll(items: Iterable<T>): this; | ||
delete(item: T): this; | ||
@@ -23,2 +24,10 @@ clear(): this; | ||
filter(predicate: (item: T) => boolean): SetWrapper<T, S>; | ||
/** | ||
* Returns the Set of all items of this Set that are also found in the passed Set. | ||
*/ | ||
intersection(other: ReadonlySet<T>): SetWrapper<T, S>; | ||
/** | ||
* Returns the Set of all items of this Set but not found in the passed Set. | ||
*/ | ||
difference(other: ReadonlySet<T>): SetWrapper<T, S>; | ||
toArray(): import("./array").ArrayWrapper<T[]>; | ||
@@ -25,0 +34,0 @@ pipe: typeof import("./lift").pipe; |
@@ -23,2 +23,5 @@ "use strict"; | ||
} | ||
addAll(items) { | ||
return new SetWrapper(new Set([...this._value, ...items])); | ||
} | ||
delete(item) { | ||
@@ -46,2 +49,14 @@ const result = this._clone(); | ||
} | ||
/** | ||
* Returns the Set of all items of this Set that are also found in the passed Set. | ||
*/ | ||
intersection(other) { | ||
return this.filter(item => other.has(item)); | ||
} | ||
/** | ||
* Returns the Set of all items of this Set but not found in the passed Set. | ||
*/ | ||
difference(other) { | ||
return this.filter(item => !other.has(item)); | ||
} | ||
toArray() { | ||
@@ -48,0 +63,0 @@ return this.pipe(s => [...s]); |
@@ -7,6 +7,5 @@ declare type UnionDescription = Record<string, (...args: any[]) => any>; | ||
}; | ||
factories: { | ||
[K in keyof T]: Factory<T[K], K> & { | ||
T: ReturnType<Factory<T[K], K>>; | ||
}; | ||
} & { | ||
[K in keyof T]: Factory<T[K], K> & { | ||
T: ReturnType<Factory<T[K], K>>; | ||
}; | ||
@@ -13,0 +12,0 @@ }; |
@@ -21,6 +21,3 @@ "use strict"; | ||
} | ||
return { | ||
factories, | ||
is | ||
}; | ||
return Object.assign(Object.assign({}, factories), { is }); | ||
} | ||
@@ -27,0 +24,0 @@ exports.createUnion = createUnion; |
@@ -22,5 +22,5 @@ import { Draft } from './immupdate'; | ||
/** | ||
* Appends an Array of items at the end of the Array. | ||
* Appends an Iterable of items at the end of the Array. | ||
*/ | ||
appendAll(items: ReadonlyArray<T[number]>): ArrayWrapper<T>; | ||
appendAll(items: Iterable<T[number]>): ArrayWrapper<T>; | ||
/** | ||
@@ -27,0 +27,0 @@ * Filters all the falsy elements out of this Array. |
@@ -34,6 +34,6 @@ import { identity } from './function'; | ||
/** | ||
* Appends an Array of items at the end of the Array. | ||
* Appends an Iterable of items at the end of the Array. | ||
*/ | ||
appendAll(items) { | ||
return new ArrayWrapper(this._value.concat(items)); | ||
return new ArrayWrapper([...this._value, ...items]); | ||
} | ||
@@ -40,0 +40,0 @@ /** |
@@ -50,10 +50,2 @@ /** | ||
/** | ||
* Efficiently inserts a string|number item in an already sorted Array. | ||
*/ | ||
insertSorted<U extends string | number>(this: DraftArray<U>, item: U): void; | ||
/** | ||
* Efficiently inserts an item in an already sorted Array. The second argument is the key on which items are sorted. | ||
*/ | ||
insertSorted(item: T, bySortKey: (item: T) => string | number): void; | ||
/** | ||
* Runs a predicate for each item of the Array. | ||
@@ -60,0 +52,0 @@ * If it returns true, a Draft item is created and given to updateFunction, ready to be mutated. |
@@ -69,19 +69,2 @@ /** | ||
insert: (item, index) => mutateArray(() => getArray().splice(index, 0, item), allKeys), | ||
insertSorted: (item, by = item => item) => mutateArray(() => { | ||
const arr = getArray(); | ||
let low = 0, high = arr.length; | ||
const itemValue = by(item); | ||
while (low < high) { | ||
const mid = (low + high) >>> 1; | ||
const midValue = by(arr[mid]); | ||
const itemValueIsBigger = typeof itemValue === 'string' | ||
? itemValue.localeCompare(midValue) > 0 | ||
: itemValue > midValue; | ||
if (itemValueIsBigger) | ||
low = mid + 1; | ||
else | ||
high = mid; | ||
} | ||
arr.splice(low, 0, item); | ||
}, allKeys), | ||
updateIf: (predicate, updateFunction) => getArray().forEach((item, index) => { | ||
@@ -88,0 +71,0 @@ if (predicate(item, index)) |
export { lift } from './lift'; | ||
export { update } from './immupdate'; | ||
export { range } from './array'; | ||
export { createUnion } from './union'; | ||
export { createUnion, empty } from './union'; | ||
export { createEnum } from './enum'; | ||
@@ -6,0 +6,0 @@ export { identity, noop } from './function'; |
@@ -5,3 +5,3 @@ import { pipe } from './lift'; | ||
export { range } from './array'; | ||
export { createUnion } from './union'; | ||
export { createUnion, empty } from './union'; | ||
export { createEnum } from './enum'; | ||
@@ -8,0 +8,0 @@ export { identity, noop } from './function'; |
@@ -0,1 +1,2 @@ | ||
import { ObjectWrapper } from './object'; | ||
import { Draft } from './immupdate'; | ||
@@ -45,2 +46,3 @@ import type { Pipe } from './lift'; | ||
toArray(): import("./array").ArrayWrapper<[K, V][]>; | ||
toObject<KK extends string | number>(this: MapWrapper<KK, V, any>): ObjectWrapper<Record<KK, V | undefined>>; | ||
} | ||
@@ -47,0 +49,0 @@ export declare function setMapPipe(_pipe: Pipe): void; |
@@ -70,2 +70,8 @@ import { update } from './immupdate'; | ||
} | ||
toObject() { | ||
return this.pipe(m => [...m].reduce((obj, [key, val]) => { | ||
obj[key] = val; | ||
return obj; | ||
}, {})); | ||
} | ||
} | ||
@@ -72,0 +78,0 @@ let pipe; |
import { ArrayWrapper } from './array'; | ||
import { MapWrapper } from './map'; | ||
import { Draft } from './immupdate'; | ||
@@ -41,3 +42,9 @@ import type { Pipe } from './lift'; | ||
update(updateFunction: (draft: Draft<T>) => void): import("./lift").Lifted<T>; | ||
/** | ||
* Transforms this Object to a Map where the keys are the string typed keys of this Object. | ||
*/ | ||
toMap(): MapWrapper<KeyAsString<keyof T>, T[keyof T], Map<KeyAsString<keyof T>, T[keyof T]>>; | ||
} | ||
export declare function setObjectPipe(_pipe: Pipe): void; | ||
declare type KeyAsString<K> = K extends string ? K : string; | ||
export {}; |
@@ -60,2 +60,8 @@ import { clone, update } from './immupdate'; | ||
} | ||
/** | ||
* Transforms this Object to a Map where the keys are the string typed keys of this Object. | ||
*/ | ||
toMap() { | ||
return this.pipe(o => new Map(Object.entries(o))); | ||
} | ||
} | ||
@@ -62,0 +68,0 @@ let pipe; |
@@ -11,2 +11,3 @@ import type { Pipe } from './lift'; | ||
add(item: T): this; | ||
addAll(items: Iterable<T>): this; | ||
delete(item: T): this; | ||
@@ -23,2 +24,10 @@ clear(): this; | ||
filter(predicate: (item: T) => boolean): SetWrapper<T, S>; | ||
/** | ||
* Returns the Set of all items of this Set that are also found in the passed Set. | ||
*/ | ||
intersection(other: ReadonlySet<T>): SetWrapper<T, S>; | ||
/** | ||
* Returns the Set of all items of this Set but not found in the passed Set. | ||
*/ | ||
difference(other: ReadonlySet<T>): SetWrapper<T, S>; | ||
toArray(): import("./array").ArrayWrapper<T[]>; | ||
@@ -25,0 +34,0 @@ pipe: typeof import("./lift").pipe; |
@@ -20,2 +20,5 @@ /** A Set wrapper providing extra functionalities and more chaining opportunities */ | ||
} | ||
addAll(items) { | ||
return new SetWrapper(new Set([...this._value, ...items])); | ||
} | ||
delete(item) { | ||
@@ -43,2 +46,14 @@ const result = this._clone(); | ||
} | ||
/** | ||
* Returns the Set of all items of this Set that are also found in the passed Set. | ||
*/ | ||
intersection(other) { | ||
return this.filter(item => other.has(item)); | ||
} | ||
/** | ||
* Returns the Set of all items of this Set but not found in the passed Set. | ||
*/ | ||
difference(other) { | ||
return this.filter(item => !other.has(item)); | ||
} | ||
toArray() { | ||
@@ -45,0 +60,0 @@ return this.pipe(s => [...s]); |
@@ -7,6 +7,5 @@ declare type UnionDescription = Record<string, (...args: any[]) => any>; | ||
}; | ||
factories: { | ||
[K in keyof T]: Factory<T[K], K> & { | ||
T: ReturnType<Factory<T[K], K>>; | ||
}; | ||
} & { | ||
[K in keyof T]: Factory<T[K], K> & { | ||
T: ReturnType<Factory<T[K], K>>; | ||
}; | ||
@@ -13,0 +12,0 @@ }; |
@@ -18,6 +18,3 @@ /** | ||
} | ||
return { | ||
factories, | ||
is | ||
}; | ||
return Object.assign(Object.assign({}, factories), { is }); | ||
} | ||
@@ -24,0 +21,0 @@ export function empty() { |
{ | ||
"name": "space-lift", | ||
"version": "1.0.0-beta.8", | ||
"version": "1.0.0-beta.9", | ||
"description": "Idiomatic TypeScript Array, Object, Map, Set, Union, Enum utils", | ||
@@ -5,0 +5,0 @@ "sideEffects": true, |
@@ -585,35 +585,33 @@ **space-lift** | ||
```ts | ||
import { createUnion, empty } from 'space-lift/es/union' | ||
import { createUnion, empty } from 'space-lift' | ||
const union = createUnion({ | ||
green: empty, | ||
orange: empty, | ||
red: empty, | ||
broken: (cause: string) => ({ cause }) | ||
}) | ||
// Let's take the example of a single input Form that can send a new message or edit an existing one. | ||
// createUnion() gives you 3 tools: | ||
// T: the derived type for the overall union | ||
// is: a typeguard function for each state | ||
// Lastly, the returned object has a key acting as a factory for each union member | ||
const formState = createUnion({ | ||
creating: empty, | ||
editing: (msgId: string) => ({ msgId }), | ||
sendingCreation: empty, | ||
sendingUpdate: (msgId: string) => ({ msgId }), | ||
}); | ||
const is = union.is | ||
const stopLight = union.factories | ||
// The initial form state is 'creating' | ||
let state: typeof formState.T = formState.creating() // { type: 'creating' } | ||
// We can use the derived type for the overall union | ||
type StopLight = typeof union.T | ||
const orange: StopLight = stopLight.orange() | ||
// If the user wants to edit an existing message, we have to store the edited message id. Lets update our state. | ||
onClickEdit(msgId: string) { | ||
state = formState.editing(msgId) // { type: 'editing', msgId: 'someId' } | ||
} | ||
// Or an individual derived type | ||
type Green = typeof stopLight.green.T | ||
const green: Green = stopLight.green() | ||
const broken = stopLight.broken('oops') | ||
// factories are provided | ||
stopLight.orange() // { type: 'orange' } | ||
stopLight.broken('oops') // { type: 'broken', cause: 'oops' } | ||
// typeguards are provided | ||
if (is('broken')(broken)) { | ||
broken.cause | ||
// In edition mode, we could want to get the message and change the send button label | ||
if (formState.is('editing')(state)) { | ||
getMessage(state.msgId) // thanks to the typeguard function, we know msgId is available in the state | ||
buttonLabel = 'Update message' | ||
} | ||
if (is('broken')(green)) { | ||
// will never get there | ||
} | ||
// If needed, we can also access the derived type of a given state | ||
type EditingType = typeof formState.editing.T | ||
const editingObj: EditingType = formState.editing('someId') | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108267
2505
617