-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from endersoncosta/master
Javascript variables identifier
- Loading branch information
Showing
6 changed files
with
188 additions
and
12 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
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,11 @@ | ||
const checker = require("../checker") | ||
const err = require("../errorCodes") | ||
|
||
function javascriptIndentifier(value) { | ||
if (checker.isEmpty(value)) return { [err.cantBeEmpty]: true } | ||
|
||
const result = checker.isValidJavascriptIdentifier(value) | ||
return result ? null: { [err.invalidJavascriptIdentifier]: true } | ||
} | ||
|
||
module.exports = javascriptIndentifier |
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,89 @@ | ||
const assert = require("assert") | ||
const { validate, errorCodes } = require("../../src/suma") | ||
const err = errorCodes | ||
|
||
describe("javascriptIdentifier validation", () => { | ||
it('does not allow empty values', () => { | ||
|
||
const samples = [ | ||
{}, | ||
null, | ||
'', | ||
undefined | ||
] | ||
for (const value of samples) { | ||
// given | ||
const validations = { javascriptIdentifier: true } | ||
// when | ||
const ret = validate(value, validations) | ||
// then | ||
assert.deepStrictEqual(ret, { value: value, errors: [{ [err.cantBeEmpty]: true }] }) | ||
} | ||
}) | ||
|
||
it("does not allow non strings", function () { | ||
|
||
const samples = [ | ||
3.14, | ||
192.168, | ||
true, | ||
{ key: "i'm a string" } | ||
] | ||
|
||
for (const value of samples) { | ||
// given | ||
const validations = { javascriptIdentifier: true } | ||
// when | ||
const ret = validate(value, validations) | ||
// then | ||
assert.deepStrictEqual(ret, { value: value, errors: [{ [err.invalidJavascriptIdentifier]: true }] }) | ||
} | ||
|
||
}) | ||
|
||
|
||
it("does not allow 'invalid' javascriptIdentifiers", function () { | ||
|
||
const samples = [ | ||
"http://", | ||
"//", | ||
"//a", | ||
"true", | ||
"false", | ||
"boolean", | ||
"1getTest", | ||
"1124", | ||
"get##", | ||
"%$@" | ||
] | ||
|
||
for (const value of samples) { | ||
// given | ||
const validations = { javascriptIdentifier: true } | ||
// when | ||
const ret = validate(value, validations) | ||
// then | ||
assert.deepStrictEqual(ret, { value: value, errors: [{ [err.invalidJavascriptIdentifier]: true }] }) | ||
} | ||
|
||
}) | ||
|
||
|
||
it('does allows valid javascriptidentifiers', () => { | ||
|
||
const samples = [ | ||
"getTest", | ||
"getTest23", | ||
"get_Test", | ||
"get_Test$", | ||
] | ||
for (const value of samples) { | ||
// given | ||
const validations = { javascriptIdentifier: true } | ||
// when | ||
const ret = validate(value, validations) | ||
// then | ||
assert.deepStrictEqual(ret, { value: value, errors: [] }) | ||
} | ||
}) | ||
}) |