Created
July 14, 2020 09:08
-
-
Save mike-hogan/ba3a5bf54f8cd25e869b129478b0266d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import {head, tail} from "../../src/lodash/lodash"; | |
function extractTermsRecursive(collector: string[], accumulatingTerm: string, depth: number, currentCharacter: string | null, remainingCharacters: string[], openingDelimiter: string, closingDelimiter: any):string[] { | |
if(currentCharacter === null) { | |
return collector | |
} | |
if(currentCharacter === openingDelimiter) { | |
return extractTermsRecursive(collector, accumulatingTerm + currentCharacter, depth + 1, head(remainingCharacters), tail(remainingCharacters), openingDelimiter, closingDelimiter) | |
} | |
if(currentCharacter === closingDelimiter) { | |
if(depth === 0) { | |
return extractTermsRecursive(collector, accumulatingTerm, depth , head(remainingCharacters), tail(remainingCharacters), openingDelimiter, closingDelimiter) | |
} | |
if(depth === 1) { | |
collector.push(accumulatingTerm + currentCharacter) | |
return extractTermsRecursive(collector, "", depth - 1 , head(remainingCharacters), tail(remainingCharacters), openingDelimiter, closingDelimiter) | |
} else { | |
return extractTermsRecursive(collector, accumulatingTerm + currentCharacter, depth - 1, head(remainingCharacters), tail(remainingCharacters), openingDelimiter, closingDelimiter) | |
} | |
} | |
if(depth === 0) { | |
return extractTermsRecursive(collector, accumulatingTerm, depth , head(remainingCharacters), tail(remainingCharacters), openingDelimiter, closingDelimiter) | |
} | |
return extractTermsRecursive(collector, accumulatingTerm + currentCharacter, depth , head(remainingCharacters), tail(remainingCharacters), openingDelimiter, closingDelimiter) | |
} | |
export function extractTerms(input: string, openingDelimiter: string, closingDelimiter:string):string[] { | |
const characters = input.split('') | |
return extractTermsRecursive([], "", 0, head(characters), tail(characters), openingDelimiter, closingDelimiter) | |
} | |
test("deals with empty strings", () => { | |
expect(extractTerms("","{", "}")).toHaveLength(0) | |
}) | |
test("deals with no matches", () => { | |
expect(extractTerms("has no braces","{", "}")).toHaveLength(0) | |
}) | |
test("deals with empty terms", () => { | |
expect(extractTerms("{} {}","{", "}")).toMatchObject(['{}','{}']) | |
}) | |
test("leading closing delimiters are ignored", () => { | |
expect(extractTerms("}{} {}","{", "}")).toMatchObject(['{}','{}']) | |
}) | |
test("tailing opening delimiters are ignored", () => { | |
expect(extractTerms("{} {}{","{", "}")).toMatchObject(['{}','{}']) | |
}) | |
test("unbalanced ends are ignored", () => { | |
expect(extractTerms("{} {} {{}","{", "}")).toMatchObject(['{}','{}']) | |
}) | |
test("deals with nested empty terms", () => { | |
expect(extractTerms("{{}} {{{}}}","{", "}")).toMatchObject(['{{}}','{{{}}}']) | |
}) | |
test("deals with nested terms with content", () => { | |
expect(extractTerms("{{a,b,c}} ignored , {{d,e:{f,g}}}","{", "}")).toMatchObject(['{{a,b,c}}','{{d,e:{f,g}}}']) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment