-
Notifications
You must be signed in to change notification settings - Fork 79
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 #874 from pano9000/refactor_sanitizeAttributeName
refactor(sanitizeAttributeName): simplify function and export
- Loading branch information
Showing
6 changed files
with
55 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import sanitizeAttributeName from "../src/services/sanitize_attribute_name" | ||
import { describe, it, execute, expect } from "./mini_test"; | ||
|
||
// fn value, expected value | ||
const testCases: [fnValue: string, expectedValue: string][] = [ | ||
["testName", "testName"], | ||
["test_name", "test_name"], | ||
["test with space", "test_with_space"], | ||
["test:with:colon", "test:with:colon"], | ||
|
||
// numbers | ||
["123456", "123456"], | ||
["123:456", "123:456"], | ||
["123456 abc", "123456_abc"], | ||
|
||
// non-latin characters | ||
["ε", "ε"], | ||
["attribute ε", "attribute_ε"], | ||
|
||
|
||
// special characters | ||
["test/name", "test_name"], | ||
["test%name", "test_name"], | ||
["\/", "_"], | ||
|
||
// empty string | ||
["", "unnamed"], | ||
] | ||
|
||
|
||
|
||
describe("sanitizeAttributeName unit tests", () => { | ||
|
||
testCases.forEach(testCase => { | ||
return it(`'${testCase[0]}' should return '${testCase[1]}'`, () => { | ||
const [value, expected] = testCase; | ||
const actual = sanitizeAttributeName(value); | ||
expect(actual).toEqual(expected); | ||
}) | ||
}) | ||
}) | ||
|
||
execute() |
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 |
---|---|---|
@@ -1,18 +1,8 @@ | ||
function sanitizeAttributeName(origName: string) { | ||
let fixedName: string; | ||
|
||
if (origName === '') { | ||
fixedName = "unnamed"; | ||
} | ||
else { | ||
export default function sanitizeAttributeName(origName: string) { | ||
const fixedName = (origName === '') | ||
? "unnamed" | ||
: origName.replace(/[^\p{L}\p{N}_:]/ug, "_"); | ||
// any not allowed character should be replaced with underscore | ||
fixedName = origName.replace(/[^\p{L}\p{N}_:]/ug, "_"); | ||
} | ||
|
||
return fixedName; | ||
} | ||
|
||
|
||
export default { | ||
sanitizeAttributeName | ||
}; | ||
} |