Skip to content

Commit

Permalink
patch(vest): Add isolate module for containing re-orders
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent bed7040 commit 94e00a8
Show file tree
Hide file tree
Showing 40 changed files with 816 additions and 212 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"test": "vx test",
"release": "vx release",
"lint": "eslint . --ignore-path .gitignore",
"prepare": "husky install"
"prepare": "husky install",
"dev": "vx dev"
},
"prettier": {
"arrowParens": "avoid",
Expand Down
4 changes: 3 additions & 1 deletion packages/n4s/src/rules/inside.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import bindNot from 'bindNot';
import { isStringValue as isString } from 'isStringValue';

import { isArray } from 'isArrayValue';

export function inside(value: unknown, arg1: string | unknown[]): boolean {
if (Array.isArray(arg1)) {
if (isArray(arg1)) {
return arg1.indexOf(value) !== -1;
}

Expand Down
33 changes: 0 additions & 33 deletions packages/shared/src/__tests__/removeElementFromArray.test.ts

This file was deleted.

70 changes: 70 additions & 0 deletions packages/shared/src/nestedArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { isArray } from 'isArrayValue';
import { isNotNull } from 'isNull';

import asArray from 'asArray';
import defaultTo from 'defaultTo';
import last from 'last';

export type NestedArray<T> = Array<NestedArray<T> | T>;

// This is sort of a map/filter in one function.
// Normally, behaves like a nested-array map
// Returning `null` will drop the element from the array
export function transform<T>(
array: NestedArray<T>,
cb: (value: T) => NestedArray<T> | T | null
): NestedArray<T> {
const res = [];
for (const v of array) {
if (isArray(v)) {
res.push(transform(v, cb));
} else {
const output = cb(v);

if (isNotNull(output)) {
res.push(output);
}
}
}
return res as NestedArray<T>;
}

export function valueAtPath<T>(
array: NestedArray<T>,
path: number[]
): T | NestedArray<T> {
return getCurrent(array, path)[last(path)];
}

export function setValueAtPath<T>(
array: NestedArray<T>,
path: number[],
value: NestedArray<T> | T
): NestedArray<T> {
const current = getCurrent(array, path);

current[last(path)] = value;
return array;
}

export function flatten<T>(values: NestedArray<T> | T): T[] {
return asArray(values).reduce((acc, value) => {
if (isArray(value)) {
return (acc as NestedArray<T>).concat(flatten(value));
}

return asArray(acc).concat(value);
}, [] as T[]) as T[];
}

export function getCurrent<T>(
array: NestedArray<T>,
path: number[]
): NestedArray<T> {
let current: NestedArray<T> = array;
for (const p of path.slice(0, -1)) {
current[p] = defaultTo(current[p], []);
current = current[p] as NestedArray<T>;
}
return current;
}
12 changes: 0 additions & 12 deletions packages/shared/src/removeElementFromArray.ts

This file was deleted.

13 changes: 9 additions & 4 deletions packages/vast/src/vast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ export function createState(
}

function reset(): void {
const prev = current();
state.references = [];
registrations.forEach(([initialValue], index) =>
initKey(index, initialValue)
initKey(index, initialValue, prev[index])
);
}

function initKey<S>(key: number, initialState?: TStateInput<S>) {
function initKey<S>(
key: number,
initialState?: TStateInput<S>,
prevState?: S | undefined
) {
current().push();
set(key, optionalFunctionValue(initialState));
set(key, optionalFunctionValue(initialState, prevState));

return function useStateKey(): TStateHandlerReturn<S> {
return [
Expand Down Expand Up @@ -83,7 +88,7 @@ export function createState(
}
}

type TStateInput<S> = S | (() => S);
type TStateInput<S> = S | ((prevState?: S) => S);
type TSetStateInput<S> = S | ((prevState: S) => S);

export type TState = ReturnType<typeof createState>;
Expand Down
Loading

0 comments on commit 94e00a8

Please sign in to comment.