Skip to content

Commit

Permalink
refactor: walk and pathToString functions
Browse files Browse the repository at this point in the history
  • Loading branch information
markkovari authored and madbence committed Oct 27, 2020
1 parent 7218c6b commit 961ff93
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
70 changes: 70 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"scripts": {
"lint": "eslint --ext ts src",
"test": "jest",
"test": "npm run build && jest",
"build": "tsc",
"coverage": "jest --coverage"
},
Expand Down Expand Up @@ -38,14 +38,29 @@
"@commitlint/config-conventional": "^11.0.0",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"@types/jest": "^26.0.15",
"eslint": "^6.8.0",
"husky": "^4.2.3",
"jest": "^26.0.1",
"ts-jest": "^26.4.1",
"typescript": "^4.0.3"
},
"engines": {
"node": ">= 12.0.0"
},
"jest": {
"roots": [
"<rootDir>/tests",
"<rootDir>/regression-tests"
],
"testMatch": [
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"preset": "ts-jest"
},
"husky": {
"hooks": {
"pre-commit": "npm run lint && npm test",
Expand Down
12 changes: 10 additions & 2 deletions src/walk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export function walk(node, path, visitor, parent?) {
export function walk(
node: any,
path: (string | number)[],
visitor: Function,
parent?: any
) {
if (typeof node === 'string' || typeof node === 'number') {
visitor(node, path, parent);
} else if (Array.isArray(node)) {
Expand All @@ -13,7 +18,10 @@ export function walk(node, path, visitor, parent?) {
}
}

export function pathToString(path) {
export function pathToString(path: (string | number)[]) {
if (!path) {
throw Error('Path should contain at least one subpath in it');
}
let str = '';
for (const segment of path) {
if (typeof segment == 'number') {
Expand Down
39 changes: 39 additions & 0 deletions tests/walk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { pathToString, walk } from '../src/walk';

describe.each([
[[], ''],
[['somepath'], '.somepath'],
[[1, 'path'], '.[1].path'],
[['some', 'path'], '.some.path'],
[['some', 'other', '2'], '.some.other.2'],
[['some', 'other', 2], '.some.other[2]'],
[['some', 'other', 2, 'suffix'], '.some.other[2].suffix'],
[['some', 'other', 2, 'suffix', 3, 'secondsuffix'], '.some.other[2].suffix[3].secondsuffix'],
])('path stringifier: ', (originalPath, expectedStringified) => {
test(`${originalPath} should be ${expectedStringified}`, () => {
expect(pathToString(originalPath)).toBe(expectedStringified);
});
});

it('stringifying empty path should throw an error', () => {
expect(() => pathToString(null)).toThrowError()
})

describe.each([
[null, [], 0],
[2, [], 1],
['two', [], 1],
[{ node: 'node' }, ['node'], 1],
[[], ['some', 'existsing', 'path'], 0],
[['existingElement'], [], 1],
[{ node: [] }, [], 0],
[['existingElement', 'anotherExistingElement'], [], 2],
[['existingElement', ['anotherExistingElement']], ['existing', 'path'], 2],
[['existingElement', ['anotherExistingElement']], [], 2],
])('should work', (node, path, expectedVisitCalls) => {
test(`Node ${JSON.stringify(node)} with path: ${path} should be visited ${expectedVisitCalls} times`, () => {
const mockVisitorFn: jest.Mock<any, any> = jest.fn();
walk(node, path, mockVisitorFn);
expect(mockVisitorFn).toHaveBeenCalledTimes(expectedVisitCalls);
});
});

0 comments on commit 961ff93

Please sign in to comment.