-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from erqk/v5
V5
- Loading branch information
Showing
8 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
projects/ng-dynamic-json-form/src/lib/utilities/get-item-key-in-array.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getValueInObject } from './get-value-in-object'; | ||
|
||
const caseA = { | ||
level_1_caseA: { | ||
level_2_caseA: { | ||
list: [ | ||
{ | ||
value: 0, | ||
}, | ||
{ | ||
value: 1, | ||
}, | ||
{ | ||
value: 2, | ||
}, | ||
{ | ||
value: 3, | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
const caseB = { | ||
level_1_caseB: { | ||
level_2_caseB: ['A', 'B', 'C'], | ||
}, | ||
}; | ||
|
||
describe('getItemKeyInArray', () => { | ||
it('get 1', () => { | ||
const index = getValueInObject( | ||
caseA, | ||
'level_1_caseA.level_2_caseA.list.["value", "===", 1].value' | ||
); | ||
|
||
expect(index).toEqual(1); | ||
}); | ||
|
||
it('get "C"', () => { | ||
const index = getValueInObject(caseB, 'level_1_caseB.level_2_caseB.[,"===", "C"]'); | ||
expect(index).toEqual('C'); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
projects/ng-dynamic-json-form/src/lib/utilities/get-item-key-in-array.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { FormControlIfCondition } from '../models'; | ||
import { getValueInObject } from './get-value-in-object'; | ||
|
||
/** | ||
* Get the key of target item if the value is an array. | ||
* Use syntax same with `FormControlIfCondition` to find the target item. | ||
* | ||
* @param array The array value | ||
* @param path [path, operator, value] | ||
* @returns key | ||
*/ | ||
export function getItemKeyInArray(array: any[], path: string): string { | ||
const validSyntax = | ||
path.startsWith('[') && path.endsWith(']') && path.split(',').length === 3; | ||
|
||
if (!validSyntax || !Array.isArray(array)) { | ||
return path; | ||
} | ||
|
||
const [key, operator, value] = path | ||
.replace('[', '') | ||
.replace(']', '') | ||
.trim() | ||
.split(',') | ||
.map((x) => x.trim()) as FormControlIfCondition; | ||
|
||
const _key = key.replaceAll('"', '').replaceAll("'", ''); | ||
const _operator = operator.replaceAll('"', '').replaceAll("'", ''); | ||
|
||
const index = array.findIndex((item) => { | ||
const left = !_key ? item : getValueInObject(item, _key); | ||
const right = JSON.parse(value); | ||
|
||
switch (_operator) { | ||
case '!==': | ||
return left !== right; | ||
|
||
case '===': | ||
return left === right; | ||
|
||
case '<': | ||
return left < right; | ||
|
||
case '>': | ||
return left > right; | ||
|
||
case '<=': | ||
return left <= right; | ||
|
||
case '>=': | ||
return left >= right; | ||
|
||
case 'includes': | ||
return left.includes(right); | ||
|
||
case 'notIncludes': | ||
return !left.includes(right); | ||
} | ||
}); | ||
|
||
return index < 0 ? '0' : index.toString(); | ||
} |
24 changes: 23 additions & 1 deletion
24
projects/ng-dynamic-json-form/src/lib/utilities/get-value-in-object.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
## 5.2.15 | ||
## 5.2.16 | ||
|
||
- [English](./v5/index_en.md) | ||
- [繁中](./v5/index_zh-TW.md) | ||
|