Skip to content

Commit

Permalink
remove: iterateEndPoint was deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenesius committed Aug 23, 2023
1 parent 402ad2b commit a748bc1
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 172 deletions.
16 changes: 9 additions & 7 deletions src/classes/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,15 @@ export default class Form extends EventEmitter implements FormDependence {
* */
if (!options.change) {
const extendName = concatName(options.executedFrom, options.target)
const extendValues = extendName ? {[extendName]: values} : values
const keys = Object.keys(plainObject(extendValues));

keys.forEach(endPointName => {
if (this.checkFieldChange(endPointName))
this.acceptChanges(endPointName)
});
const extendValues = extendName ? {[extendName]: values} : values;

console.log('EXTEND VALUES', extendValues)
bypassObject(extendValues)
.forEach(point => {
console.log(extendValues, point, '++')
if (this.checkFieldChange(point.name))
this.acceptChanges(point.name)
})
}

// После того как изменения были спроецированы на формы, происходит создание события и уведомления всех дочерних
Expand Down
62 changes: 35 additions & 27 deletions src/utils/bypass-object.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import isEndPointValue from "./is-end-point-value";
import concatName from "./concat-name";
import isIterablePoint from "./is-iterable-point";
import splitName from "./split-name";

function step(array: BypassItem[], value: any, path: string[] = []): void {
if (isEndPointValue(value)) return;

Object.keys(value)
.forEach(key => {

const parsedKey = key.split('.');

const p = [...path, ...parsedKey]; // Step path
const v = value[key]; // Step value

if (isEndPointValue(v)) {
array.push({
path: p,
value: v
})
return;
}

step(array, v, p)
})
}
/**
* @description Функция проходит по всем конечным полям объекта.
* @description Функция проходит по всем конечным элементам объекта.
* @return Array of {path: string[], value: any}
* @example
* { person: { profile: { head: { mouth: 1, eyes: 2 } } } }
Expand All @@ -41,15 +21,43 @@ function step(array: BypassItem[], value: any, path: string[] = []): void {
*/
export default function bypassObject(object: any): BypassItem[] {
const array:BypassItem[] = [];

step(array, object);

return array
}

function step(array: BypassItem[], value: any, path: string[] = []): void {
if (!isIterablePoint(value)) return;

Object.keys(value)
.forEach(key => {

const parsedKey = splitName(key);

const p = [...path, ...parsedKey]; // Step path
const v = value[key]; // Step value

if (!isIterablePoint(v)) {
array.push({
path: p,
value: v,
name: concatName(...p),
set: (newValue) => value[key] = newValue
})
return;
}

step(array, v, p)
})
}

interface BypassItem {
value: any,
path: string[]
path: string[],
name: string,

set: (x: any) => void
}


Expand Down
9 changes: 8 additions & 1 deletion src/utils/check-deep-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import checkPrimitiveValue from "./check-primitive-value";

/**
* @description Вернёт true если возможно перебрать значение вглубь.
* */
export default function checkDeepValue(value: unknown) {
return !checkPrimitiveValue(value) && !Array.isArray(value) && !Object.isFrozen(value);
return !(
checkPrimitiveValue(value) ||
Array.isArray(value) ||
Object.isFrozen(value)
) ;
}
10 changes: 10 additions & 0 deletions src/utils/is-iterable-point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import isEndPointValue from "./is-end-point-value";
import isEmptyObject from "./is-empty-object";

/**
* @description Функция используется для логики формы. Т.к. нам часто нужно проходить по объектам и перебирать их свойства,
* то это функция отвечает на вопрос: Является ли переданное значение итерируемым и можно ли зайти внутрь его.
* */
export default function isIterablePoint(value: unknown) {
return !(isEndPointValue(value) || isEmptyObject(value))
}
43 changes: 0 additions & 43 deletions src/utils/iterate-endpoint.ts

This file was deleted.

23 changes: 2 additions & 21 deletions src/utils/replace-values.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import {Values} from "../types";
import isEndPointValue from "./is-end-point-value";
import iterateEndpoint from "./iterate-endpoint";
import generateFieldByPath from "./generate-field-by-path";
import bypassObject from "./bypass-object";

/**
* @description Метод вернёт новый объект, заменив все примитивные значения
* на переданный аргумент.
* */

function replace(o: Values, value: any): {} {
Object.keys(o)
.forEach(key => {
if (isEndPointValue(o[key])) return o[key] = value;

replace(o[key], value);
})

return o;
}

export default function replaceValues(object: Values, value: any = true) {

return iterateEndpoint(object)
return bypassObject(object)
.reduce((acc: any, item) => {
generateFieldByPath(acc, item.path, value)
return acc;
}, {})

/*
const copyObject = JSON.parse(JSON.stringify(object));
return replace(copyObject, value);*/

}
2 changes: 0 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import insertByName from "./insert-by-name";
import isEmptyObject from "./is-empty-object";
import isEndPointValue from "./is-end-point-value";
import isPrefixName from "./is-prefix-name";
import iterateEndpoint from "./iterate-endpoint";
import iteratePoints from "./iterate-points";
import mergeObjects from "./merge-objects";
import parseFirstName from "./parse-first-name";
Expand Down Expand Up @@ -60,7 +59,6 @@ const utils = {
isEmptyObject,
isEndPointValue,
isPrefixName,
iterateEndpoint,
iteratePoints,
mergeObjects,
parseFirstName,
Expand Down
87 changes: 81 additions & 6 deletions tests/units/utils/bypass-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ describe("Bypass object", () => {
expect(bypassObject(v)).toEqual([
{
path: ['name'],
value: 'Jenesius'
value: 'Jenesius',
name: 'name',
set: expect.any(Function)
}
])

Expand All @@ -30,7 +32,9 @@ describe("Bypass object", () => {
expect(bypassObject(v)).toEqual([
{
path: ['address', 'city', 'index'],
value: 11011
value: 11011,
name: "address.city.index",
set: expect.any(Function)
}
])

Expand All @@ -52,11 +56,15 @@ describe("Bypass object", () => {
expect(bypassObject(v)).toEqual([
{
path: ['person', 'profile', 'head', 'mouth'],
value: 1
value: 1,
name: "person.profile.head.mouth",
set: expect.any(Function)
},
{
path: ['person', 'profile', 'head', 'eyes'],
value: 2
value: 2,
name: "person.profile.head.eyes",
set: expect.any(Function)
}
])

Expand All @@ -69,10 +77,77 @@ describe("Bypass object", () => {
expect(bypassObject(v)).toEqual([
{
path: ['address','name'],
value: null
value: null,
name: "address.name",
set: expect.any(Function)
}
])

})


test("One field iteration", () => {
const values = {
name: 'test'
}
expect(bypassObject(values)).toEqual([
{
value: 'test',
path: ['name'],
name: 'name',
set: expect.any(Function)
}
])
})
test("Deepen object", () => {
jest.fn()
const values = {
address: {
city: "Berlin"
}
}
// .map(a => ({value: a.value, path: a.path}))
expect(bypassObject(values)).toEqual(
[
{
value: 'Berlin',
path: ['address', 'city'],
name: 'address.city',
set: expect.any(Function)
}
]
)
})
test("Setting values", () => {

const values = {
a: 1,
b: 2,
c: 3
}
const array = bypassObject(values).map(a => ++a.value);
expect(array).toEqual([2,3,4])
})
test('Values of data', () => {
const values = {
a: 1,
b: "Jenesius",
c: 1
}
expect(bypassObject(values).map(a => a.value)).toEqual([1, "Jenesius", 1])
})
test("plain name", () => {
const values = {
"address.city": "Berlin"
}
expect(bypassObject(values)).toEqual(
[
{
value: 'Berlin',
path: ["address", "city"],
name: "address.city",
set: expect.any(Function)
}
]
)
})
})
Loading

0 comments on commit a748bc1

Please sign in to comment.