-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored split and OutputReference.
Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
7 changed files
with
141 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { split } from "helpers" | ||
|
||
export class OutputReference { | ||
chapter_id: string | ||
output_name: string|undefined | ||
|
||
constructor(chapter_id: string, output_name: string | undefined = undefined) { | ||
this.chapter_id = chapter_id | ||
this.output_name = output_name | ||
} | ||
|
||
/** | ||
* Parses a string and returns a collection of output references that it may contain. | ||
* | ||
* @param str a string | ||
* @returns an array of output references | ||
*/ | ||
static parse(str: string): OutputReference[] { | ||
const pattern = /\$\{([^}]+)\}/g | ||
let match | ||
var result = [] | ||
while ((match = pattern.exec(str)) !== null) { | ||
const spl = split(match[1], '.', 2) | ||
result.push(new OutputReference(spl[0], spl[1])) | ||
} | ||
return result | ||
} | ||
|
||
/** | ||
* Replaces occurrences of output references. | ||
* Takes special care of preserving the type of the reference value if the entire string is a reference. | ||
* | ||
* @param str a string that may contain output references | ||
* @param callback a callback for fetching reference values | ||
* @returns a string with output references replaced by their values | ||
*/ | ||
static replace(str: string, callback: (chapter_id: any, variable: any) => string): any { | ||
let full_match = str.match(/^\$\{([^}]+)\}$/) | ||
if (full_match) { | ||
// the entire string is a reference, preserve the type of the returned value | ||
const spl = split(full_match[1], '.', 2) | ||
return callback(spl[0], spl[1]) | ||
} else return str.replace(/\$\{([^}]+)\}/g, (_, k) => { | ||
const spl = split(k, '.', 2) | ||
return callback(spl[0], spl[1]) | ||
}) | ||
} | ||
} |
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
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,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { OutputReference } from "tester/OutputReference"; | ||
|
||
describe('OutputReference', () => { | ||
let f = (id: any, k: any): string => `[${id}:${k}]` | ||
|
||
describe('parse', () => { | ||
it('replaces', () => { | ||
expect(OutputReference.parse('string')).toEqual([]) | ||
expect(OutputReference.parse('${k.v}')).toEqual([new OutputReference('k', 'v')]) | ||
expect(OutputReference.parse('${k.value}')).toEqual([new OutputReference('k', 'value')]) | ||
expect(OutputReference.parse('${k.v.m}')).toEqual([new OutputReference('k', 'v.m')]) | ||
expect(OutputReference.parse('A reference to ${k.v.m} and ${x} and ${x.y}.')).toEqual([ | ||
new OutputReference('k', 'v.m'), | ||
new OutputReference('x'), | ||
new OutputReference('x', 'y') | ||
]) | ||
}) | ||
}) | ||
|
||
describe('replace', () => { | ||
it('replaces', () => { | ||
expect(OutputReference.replace('string', f)).toEqual('string') | ||
expect(OutputReference.replace('${k.v}', f)).toEqual('[k:v]') | ||
expect(OutputReference.replace('${k.value}', f)).toEqual('[k:value]') | ||
expect(OutputReference.replace('${k.v.m}', f)).toEqual('[k:v.m]') | ||
expect(OutputReference.replace('A reference to ${k.v.m} and ${x} and ${x.y}.', f)).toEqual( | ||
'A reference to [k:v.m] and [x:undefined] and [x:y].' | ||
) | ||
}) | ||
}) | ||
}); |
This file was deleted.
Oops, something went wrong.