Skip to content

Commit

Permalink
Update RegEx to remove non-alphanumeric chars (#6)
Browse files Browse the repository at this point in the history
* Update RegEx to remove non-alphanumeric chars

* Add tests for names.clean()
  • Loading branch information
sbstjn committed Jul 20, 2017
1 parent a0a89ff commit 2e023af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/aws/names.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const util = require('util')

const clean = (input) => input.replace(/\W/g, '')
const clean = (input) => input.replace(/[^a-z0-9+]+/gi, '')

const policyScale = (table, read) => clean(util.format('Table%sScalingPolicy-%s', read ? 'Read' : 'Write', table))
const policyRole = (table) => clean(util.format('DynamoDBAutoscalePolicy-%s', table))
Expand All @@ -9,4 +9,4 @@ const target = (table, read) => clean(util.format('AutoScalingTarget%s-%s', read
const metric = (read) => clean(util.format('DynamoDB%sCapacityUtilization', read ? 'Read' : 'Write'))
const role = (table) => clean(util.format('DynamoDBAutoscaleRole-%s', table))

module.exports = { dimension, metric, policyScale, policyRole, role, target }
module.exports = { dimension, metric, policyScale, policyRole, role, target, clean }
10 changes: 10 additions & 0 deletions test/aws/names.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const names = require('../../src/aws/names')

describe('Clean', () => {
it('removes non alphanumeric characters', () => {
expect(names.clean('a-b-c')).toBe('abc')
expect(names.clean('a-b_c')).toBe('abc')
expect(names.clean('A-b_9')).toBe('Ab9')
expect(names.clean('A/b*9')).toBe('Ab9')
expect(names.clean('Ä-ç_9')).toBe('9')
})
})

describe('Names', () => {
it('creates name for Role', () => {
expect(names.role('test-with-invalid-characters')).toBe('DynamoDBAutoscaleRoletestwithinvalidcharacters')
Expand Down

0 comments on commit 2e023af

Please sign in to comment.