Skip to content

Commit

Permalink
feat: add chance.js for variables with random value.
Browse files Browse the repository at this point in the history
  • Loading branch information
svrnm committed Jan 3, 2022
1 parent 78c264f commit 8fb2b16
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"ace-builds": "^1.4.13",
"axios": "^0.24.0",
"chance": "^1.1.8",
"color": "^4.1.0",
"color-name": "^1.1.4",
"color-string": "^1.9.0",
Expand Down
3 changes: 2 additions & 1 deletion src/models/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Configuration {
this.enabled = enabled
this.values = values
this.featureFlags = featureFlags

this.globalVariables = Array.isArray(globalVariables) ? globalVariables : []
}

Expand Down Expand Up @@ -265,6 +264,8 @@ class Configuration {
return carry.concat(variable)
}, [])

variables.push(new Variable('==CHANCE=JS==', '', '', global))

if (bindValues) {
return variables.map((variable) => {
return variable.bind(this.getValue(variable.owner, variable.name))
Expand Down
25 changes: 22 additions & 3 deletions src/models/Variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,43 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import chance from 'chance'
import json5 from 'json5'

class Variable {
constructor(name, placeholder, description, owner = '') {
this.name = name
this.value = placeholder
this.description = description
this.owner = owner
this.id = this.owner === '' ? this.name : this.owner + '::' + this.name
this.chanceGenerator = chance.Chance()
}

bind(value) {
return new Variable(this.name, typeof value === 'string' ? value : this.value, this.description, this.owner)
}

chance(value) {
return value.replace(/\${chance.([a-zA-Z0-9_-]*)(?:\((.*)\))?}/, (match, p1, p2) => {
let args
try {
args = json5.parse(p2)
} catch (e) {
args = ''
}
return this.chanceGenerator[p1](args)
})
}

apply(value) {
if (typeof value === 'string') {
return value.replace('$' + this.name, this.value).replace('${' + this.name + '}', this.value)
if (typeof value !== 'string') {
return value
}
if (this.name === '==CHANCE=JS==') {
return this.chance(value)
}
return value
return value.replace('$' + this.name, this.value).replace('${' + this.name + '}', this.value)
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/models/Variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,16 @@ describe('Variable', function () {
assert.strictEqual(v2.id, 'owner::name')
})
})

describe('#chance', function () {
it('creates random values with chance.js`', function () {
const v1 = new Variable('==CHANCE=JS==', '', '')
// eslint-disable-next-line no-template-curly-in-string
assert.strictEqual(v1.chance('My magic number is ${chance.integer({min: 7, max: 7})}'), 'My magic number is 7')
// eslint-disable-next-line no-template-curly-in-string
assert.strictEqual(v1.chance('${chance.character({pool: "b"})}'), 'b')
// eslint-disable-next-line no-template-curly-in-string
assert(['false', 'null', 'undefined', '0', 'NaN', ''].includes(v1.chance('${chance.falsy()}')))
})
})
})

0 comments on commit 8fb2b16

Please sign in to comment.