Skip to content

Instantly share code, notes, and snippets.

@azat-co azat-co/bash-str.js
Created Feb 7, 2017

Embed
What would you like to do?
const assert = require('assert')
const solution = (str) => {
const checkSubs = (s, oldResults=[]) => {
let start = s.indexOf('{')
let end = s.indexOf('}', start+1)
// console.log('yo', s, start, end,s.substring(end+1, s.length))
if (start<0 && end <0) {
for (let i = 0; i<oldResults.length; i++) {
oldResults[i] += s
}
return oldResults
} else {
let pattern = s.substring(start+1, end).split(',')
let results = []
for (let i=0; i<pattern.length; i++) {
if (oldResults.length == 0) results.push(s.substring(0, start)+pattern[i])
else for (let j=0; j<oldResults.length; j++) {
results.push(oldResults[j] + s.substring(0, start)+pattern[i])
}
}
// console.log(pattern, results)
return checkSubs(s.substring(end+1, s.length), results)
}
}
return checkSubs(str, [])
}
assert.deepEqual(['hello_1_A', 'hello_2_A', 'hello_3_A', 'hello_1_B', 'hello_2_B', 'hello_3_B'], solution('hello_{1,2,3}_{A,B}'))
assert.deepEqual(['1@m!', '2@m!', '1@l!', '2@l!', '1@m!', '2@m!'], solution('{1,2}@{m,l,m}!'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.