Skip to content

Commit

Permalink
sanitize model name (+ tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
asednev committed Apr 4, 2021
1 parent bc6a836 commit ca010a5
Show file tree
Hide file tree
Showing 7 changed files with 5,666 additions and 1,101 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
indent_style = space
indent_size = 2
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
Expand All @@ -25,6 +25,9 @@ jobs:
- name: Lint the project
run: npm run lint

- name: Test the project
run: npm run test

- name: Build the project
run: npm run build
env:
Expand Down
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/src/**/*.spec.ts'],
};
6,657 changes: 5,558 additions & 1,099 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"lint": "eslint src/**.ts",
"watch": "npm run build && nodemon",
"build": "rimraf ./dist && tsc",
"prepublishOnly": "npm run lint && npm run build"
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "jest --ci",
"prepublishOnly": "npm run lint && npm run test:ci && npm run build"
},
"keywords": [
"homebridge-plugin",
Expand All @@ -37,13 +40,16 @@
"govee-bt-client": "1.0.11"
},
"devDependencies": {
"@types/jest": "^26.0.21",
"@types/node": "^14.11.2",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"eslint": "^7.10.0",
"jest": "^26.6.3",
"homebridge": "^1.2.3",
"nodemon": "^2.0.4",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.4",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
}
Expand Down
19 changes: 19 additions & 0 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { sanitizeDeviceName } from "./utils";

describe("sanitizeDeviceName", () => {
it("should handle mixed case", () => {
const original = "Ab3dE";
const s = sanitizeDeviceName(original);
expect(s).toEqual(original);
});

it("should ignore underscore", () => {
const s = sanitizeDeviceName("a_b");
expect(s).toEqual("ab");
});

it("should ignore disallowed characters", () => {
const s = sanitizeDeviceName("abцd");
expect(s).toEqual("abd");
});
});
71 changes: 71 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const lowerAlpha = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
];
const upperAlpha = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
];
const numeric = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

const allowedCharsSet = new Set([...lowerAlpha, ...upperAlpha, ...numeric]);

export function sanitizeDeviceName(s: string): string {
const out: string[] = [];
for (const c of s) {
if (!allowedCharsSet.has(c)) {
continue;
}
out.push(c);
}

return out.join("");
}

0 comments on commit ca010a5

Please sign in to comment.