MCPcopy
hub / github.com/formatjs/formatjs / PartitionPattern

Function PartitionPattern

packages/ecma402-abstract/PartitionPattern.ts:8–39  ·  view source on GitHub ↗
(
  pattern: string
)

Source from the content-addressed store, hash-verified

6 * @param pattern
7 */
8export function PartitionPattern<T extends string>(
9 pattern: string
10): Array<{type: T; value: string | undefined}> {
11 const result = []
12 let beginIndex = pattern.indexOf('{')
13 let endIndex = 0
14 let nextIndex = 0
15 const length = pattern.length
16 while (beginIndex < pattern.length && beginIndex > -1) {
17 endIndex = pattern.indexOf('}', beginIndex)
18 invariant(endIndex > beginIndex, `Invalid pattern ${pattern}`)
19 if (beginIndex > nextIndex) {
20 result.push({
21 type: 'literal' as T,
22 value: pattern.substring(nextIndex, beginIndex),
23 })
24 }
25 result.push({
26 type: pattern.substring(beginIndex + 1, endIndex) as T,
27 value: undefined,
28 })
29 nextIndex = endIndex + 1
30 beginIndex = pattern.indexOf('{', nextIndex)
31 }
32 if (nextIndex < length) {
33 result.push({
34 type: 'literal' as T,
35 value: pattern.substring(nextIndex, length),
36 })
37 }
38 return result
39}

Callers 5

PartitionDateTimePatternFunction · 0.85
MakePartsListFunction · 0.85
deconstructPatternFunction · 0.85

Calls 1

invariantFunction · 0.70

Tested by

no test coverage detected