-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathunique-attribute-value-name.function.ts
More file actions
47 lines (40 loc) · 1.39 KB
/
unique-attribute-value-name.function.ts
File metadata and controls
47 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @module expression
*/
import { attributeNameReplacer } from './attribute-name-replacer.function'
/**
* @hidden
*/
export const BRACED_INDEX_REGEX = /\[(\d+)]/g
/**
* Creates a unique attribute value placeholder name to use in the expression
*
* @returns {string} The unique attribute value placeholder name in respect to the given existing value names (no duplicates allowed)
* @hidden
*/
export function uniqueAttributeValueName(key: string, existingValueNames?: string[]): string {
key = key.replace(/\./g, '__').replace(BRACED_INDEX_REGEX, attributeNameReplacer)
let potentialName = `:${key}`
let idx = 1
if (existingValueNames && existingValueNames.length) {
while (existingValueNames.includes(potentialName)) {
idx++
potentialName = `:${key}_${idx}`
}
}
return potentialName
}
export function uniqueAttributeValueNameRecord(key: string, existingValueNames?: Record<string, string>): string {
key = key.replace(/\./g, '__').replace(BRACED_INDEX_REGEX, attributeNameReplacer)
let potentialName = `:${key}`
let idx = 1
if (existingValueNames && existingValueNames.length) {
const recordKeys = Object.keys(existingValueNames)
const recordValues = Object.values(existingValueNames)
while (recordKeys.includes(potentialName) || recordValues.includes(potentialName)) {
idx++
potentialName = `:${key}_${idx}`
}
}
return potentialName
}