Skip to content

Commit

Permalink
feat: new util findNearestPrefixFromArray, update available method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenesius committed Aug 6, 2023
1 parent 9371cf7 commit 2585def
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
65 changes: 60 additions & 5 deletions src/classes/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import recursiveRemoveProp from "../utils/recursive-remove-prop";
import runPromiseQueue from "../utils/run-promise-queue";
import isPrefixName from "../utils/is-prefix-name";
import findNearestNameFromArray from "../utils/find-nearest-name-from-array";
import findNearestPrefixFromArray from "../utils/find-nearest-prefix-from-array";

/**
* Main principe : GMN
Expand Down Expand Up @@ -601,6 +602,13 @@ export default class Form extends EventEmitter implements FormDependence {
debug.msg(`Enabling ${names || ''}`);
this.available(true, names)
}

/**
* Здесь принцип отличается от setValues. Он не является оптимизированным, однако является 100% рабочим.
* В будущем будем оптимизировать.
* Мы сперва строем выходной объект availability, а затем идём по dependencies и уведомляем их, если они были изменены.
* Далее передаём объект в dispatchEvent.
* */
available(type: boolean, names?: string[] | string ) {
console.group(`AVAILABLE %c${type}`, 'color: purple')
if (typeof names === "string") names = [names];
Expand Down Expand Up @@ -632,7 +640,48 @@ export default class Form extends EventEmitter implements FormDependence {
"user.customer.id": true
}

console.log(nameState, type, av);
const copyAV = copyObject(av);
console.log('Old availability', copyAV)

/**MERGIN DATA*/

// @ts-ignore
nameState.forEach(name => av[name] = true)

Object
.keys(av)
.forEach(key => {
if (nameState.find(name => isPrefixName(key, name) || name === key)) {
// @ts-ignore
av[key] = type;
}
})

console.log("Merging:", copyObject(av));
/**MERGE END*/

/**OPTIMIZATION*/

const notOptimizeNames = Object.keys(av);

notOptimizeNames
.forEach(key => {
const nearestAvailability = findNearestPrefixFromArray(key, notOptimizeNames);
// @ts-ignore
if (!nearestAvailability && av[key] === true) return delete av[key];

if (nearestAvailability) {
// @ts-ignore
if (av[nearestAvailability] === av[key] || av[nearestAvailability] === undefined) return delete av[key];

}

})

console.log("Optimization:", copyObject(av))
/**OPTIMIZATION END*/

/*
const changes: any = []
Expand All @@ -643,12 +692,14 @@ export default class Form extends EventEmitter implements FormDependence {
const fieldForUpdate = [];
// Если Disable, и такого поля у нас нет - добавляем его
if (type === false && !Object.prototype.hasOwnProperty.call(av, name)) {
// Если для текущего поля нет информации - получаем ближайшее к этому поле родителя.
if (!Object.prototype.hasOwnProperty.call(av, name)) {
const nearestName = findNearestNameFromArray(name, _stateNames)

if (!nearestName || av[nearestName] !== type) fieldForUpdate.push(name)
// Если нет родителя и поле блокируется (т.к. по умолчанию у нас все поля true)
// Если ближайшее поле есть, но тип у них не совпадает с новым
if ((!nearestName && type === false) || (nearestName && av[nearestName] !== type)) fieldForUpdate.push(name)
}
fieldForUpdate.push(
Expand All @@ -658,7 +709,11 @@ export default class Form extends EventEmitter implements FormDependence {
console.groupEnd()
})
*/

// MERGING availability, и упрощение его если child === parent
// Было: address.city = false, address = true, address.city.index = false
// Стало: address = true, address.city = false

// names.forEach(name => this.availableByName(name, type)) ;
console.groupEnd();
Expand Down
13 changes: 13 additions & 0 deletions src/utils/find-nearest-prefix-from-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import isPrefixName from "./is-prefix-name";

export default function findNearestPrefixFromArray<T extends string>(name: string, arrayPrefixes: T[]): T | undefined {
let answer: T | undefined;

arrayPrefixes.forEach(prefix => {

if (!isPrefixName(name, prefix)) return;
if (prefix.length > (answer?.length || 0)) answer = prefix;
})

return answer?.length ? answer : undefined
}
2 changes: 1 addition & 1 deletion src/utils/is-prefix-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* @example position.city.type city -> false
* */
export default function isPrefixName(fieldName: string, prefix: string) {
return (new RegExp(`^${prefix}\.`)).test(fieldName);
return (new RegExp(`^${prefix}\\.`)).test(fieldName);
}
19 changes: 19 additions & 0 deletions tests/units/utils/find-nearest-prefix-from-array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import findNearestPrefixFromArray from "../../../src/utils/find-nearest-prefix-from-array";

describe("Nearest name", () => {

test("Name is founded.", () => {
expect(findNearestPrefixFromArray('address.city', ['city', 'address', 'address.city'])).toBe('address')
})

test("Name not founded", () => {
expect(findNearestPrefixFromArray('address.city', ['a', 'b', 'city', 'addre'])).toBe(undefined)
})

test("Founded more nearest", () => {
expect(findNearestPrefixFromArray('address.city.name', ['address', 'city', 'address.city'])).toBe('address.city');
})
test("Founded more nearest with other order", () => {
expect(findNearestPrefixFromArray('address.city.name', ['address.city', 'address', 'city'])).toBe('address.city');
})
})
4 changes: 4 additions & 0 deletions tests/units/utils/is-prefix-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ describe("Is prefix name", () => {
test("Should return false if second param is not prefix", () => {
expect(isPrefixName("position.city.type", "city")).toBe(false)
})
test("Should return false is prefix is not full name", () => {
expect(isPrefixName("position.city", "posit")).toBe(false)
expect(isPrefixName("position.city", "position.cit")).toBe(false)
})
})

0 comments on commit 2585def

Please sign in to comment.