Skip to content

Commit 105dfa8

Browse files
committed
Initial addition
1 parent ad70865 commit 105dfa8

File tree

9 files changed

+136
-40
lines changed

9 files changed

+136
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
*.d.ts
1414
*.d.ts.map
1515
!/lib/advanced-types.d.ts
16+
!/index.d.ts
1617

1718
# Library specific ones

README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# Node Module Template
1+
# chai-posix-path
22

3-
A GitHub template repo for node modules
3+
Checks whether a string path matches a posix path
44

5-
<!--
6-
[![npm version](https://img.shields.io/npm/v/buffered-async-iterable.svg?style=flat)](https://www.npmjs.com/package/buffered-async-iterable)
7-
[![npm downloads](https://img.shields.io/npm/dm/buffered-async-iterable.svg?style=flat)](https://www.npmjs.com/package/buffered-async-iterable)
8-
-->
5+
[![npm version](https://img.shields.io/npm/v/chai-posix-path.svg?style=flat)](https://www.npmjs.com/package/chai-posix-path)
6+
[![npm downloads](https://img.shields.io/npm/dm/chai-posix-path.svg?style=flat)](https://www.npmjs.com/package/chai-posix-path)
97
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/voxpelli/eslint-config)
108
[![Module type: ESM](https://img.shields.io/badge/module%20type-esm-brightgreen)](https://github.com/voxpelli/badges-cjs-esm)
119
[![Types in JS](https://img.shields.io/badge/types_in_js-yes-brightgreen)](https://github.com/voxpelli/types-in-js)
@@ -16,22 +14,14 @@ A GitHub template repo for node modules
1614
### Simple
1715

1816
```javascript
19-
import { something } from '@voxpelli/node-module-template';
17+
import chaiPosixPath from 'chai-posix-path';
2018

21-
// Use that something
22-
```
23-
24-
## API
25-
26-
### `something(input, { configParam }) => Promise<output>`
19+
chai.use(chaiPosixPath);
2720

28-
Takes a value (`input`), does something configured by the config (`configParam`) and returns the processed value asyncly(`output`)
29-
30-
## Similar modules
31-
32-
* [`example`](https://example.com/) – is similar in this way
21+
'foo\\bar'.should.be.posixPath('foo/bar'); // Passes
22+
'foo/bar'.should.be.posixPath('foo/bar'); // Passes
23+
```
3324

3425
## See also
3526

36-
* [Announcement blog post](#)
37-
* [Announcement tweet](#)
27+
* [Chai](https://www.chaijs.com/)

declaration.tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
{
33
"extends": "./tsconfig",
4+
"files": [],
45
"exclude": [
56
"test/**/*.js"
67
],

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default } from "./lib/index.js";
2+
export type {} from './lib/advanced-types.js';

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
/**
2-
* @returns {Promise<void>}
3-
*/
4-
export async function something () {}
1+
export { default } from './lib/index.js';

lib/advanced-types.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference types="chai" />
2+
3+
declare namespace Chai {
4+
// For BDD API
5+
interface Assertion extends LanguageChains, NumericComparison, TypeComparison {
6+
posixPath(path: string): Assertion
7+
}
8+
}

lib/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** @type {Parameters<import('chai')["use"]>[0]} */
2+
const chaiPosixPath = (chai, _utils) => {
3+
const Assertion = chai.Assertion;
4+
5+
Assertion.addMethod('posixPath', function (targetPath) {
6+
const obj = this._obj;
7+
8+
new Assertion(this._obj).to.be.a('string');
9+
new Assertion(targetPath).to.be.a('string');
10+
11+
const posixPath = obj.split('\\').join('/');
12+
13+
this.assert(
14+
posixPath === targetPath,
15+
'expected #{this} to be #{exp} but got #{act}',
16+
'expected #{this} to not be matching #{act}',
17+
targetPath, // expected
18+
posixPath // actual
19+
);
20+
});
21+
};
22+
23+
export default chaiPosixPath;

package.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
{
2-
"name": "@voxpelli/node-module-template",
2+
"name": "@voxpelli/chai-posix-path",
33
"version": "0.0.0",
4-
"description": "A GitHub template repo for node modules",
5-
"homepage": "http://github.com/voxpelli/node-module-template",
4+
"description": "Checks whether a string path matches a posix path",
5+
"homepage": "http://github.com/voxpelli/chai-posix-path",
66
"repository": {
77
"type": "git",
8-
"url": "git://github.com/voxpelli/node-module-template.git"
8+
"url": "git://github.com/voxpelli/chai-posix-path.git"
99
},
10-
"keywords": [],
10+
"keywords": [
11+
"chai",
12+
"chai-plugin",
13+
"path",
14+
"separator",
15+
"slash",
16+
"backslash",
17+
"posix"
18+
],
1119
"author": "Pelle Wessman <pelle@kodfabrik.se> (http://kodfabrik.se/)",
1220
"license": "MIT",
1321
"engines": {
@@ -32,9 +40,8 @@
3240
"check:knip": "knip",
3341
"check:lint": "eslint --report-unused-disable-directives .",
3442
"check:tsc": "tsc",
35-
"check:type-coverage": "type-coverage --detail --strict --at-least 99 --ignore-files 'test/*'",
3643
"check": "run-s clean && run-p check:*",
37-
"clean:declarations-top": "rm -rf $(find . -maxdepth 1 -type f -name '*.d.ts*' ! -name 'advanced-types.d.ts')",
44+
"clean:declarations-top": "rm -rf $(find . -maxdepth 1 -type f -name '*.d.ts*' ! -name 'index.d.ts')",
3845
"clean:declarations-lib": "rm -rf $(find lib -type f -name '*.d.ts*' ! -name 'advanced-types.d.ts')",
3946
"clean": "run-p clean:*",
4047
"prepare": "husky install",
@@ -43,8 +50,6 @@
4350
"test-ci": "run-s test:*",
4451
"test": "run-s check test:*"
4552
},
46-
"dependencies": {
47-
},
4853
"devDependencies": {
4954
"@types/chai": "^4.3.5",
5055
"@types/mocha": "^10.0.1",
@@ -68,7 +73,6 @@
6873
"knip": "^2.15.2",
6974
"mocha": "^10.2.0",
7075
"npm-run-all2": "^6.0.6",
71-
"type-coverage": "^2.26.0",
7276
"typescript": "~5.1.6"
7377
}
7478
}

test/main.spec.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,81 @@
1-
import chai from 'chai';
1+
import chai, { AssertionError } from 'chai';
22

3-
import { something } from '../index.js';
3+
import chaiPosixPath from '../index.js';
44

5-
chai.should();
5+
chai.use(chaiPosixPath);
66

7-
describe('something', () => {
8-
it('should work', async () => {
9-
await something();
7+
const should = chai.should();
8+
9+
describe('chaiPosixPath', () => {
10+
it('should handle win32 path', () => {
11+
'foo\\bar'.should.be.posixPath('foo/bar');
12+
});
13+
14+
it('should handle posix path', () => {
15+
'foo/bar'.should.be.posixPath('foo/bar');
16+
});
17+
18+
it('should handle chaining', () => {
19+
'foo\\bar'.should.be.posixPath('foo/bar').that.is.a('string');
20+
});
21+
22+
it('should handle negating path', () => {
23+
'foo\\bar'.should.not.be.posixPath('foo');
24+
});
25+
26+
it('should throw on mismatch', () => {
27+
should.Throw(
28+
() => {
29+
'foo\\bar'.should.be.posixPath('foo');
30+
},
31+
AssertionError,
32+
// eslint-disable-next-line quotes
33+
`expected 'foo\\bar' to be 'foo' but got 'foo/bar'`
34+
);
35+
});
36+
37+
it('should throw on negative mismatch', () => {
38+
should.Throw(
39+
() => {
40+
'foo\\bar'.should.not.be.posixPath('foo/bar');
41+
},
42+
AssertionError,
43+
// eslint-disable-next-line quotes
44+
`expected 'foo\\bar' to not be matching 'foo/bar'`
45+
);
46+
});
47+
48+
it('should throw on invalid entry value', () => {
49+
should.Throw(
50+
() => {
51+
const value = 123;
52+
value.should.be.posixPath('foo');
53+
},
54+
AssertionError,
55+
'expected 123 to be a string'
56+
);
57+
});
58+
59+
it('should throw on invalid target value', () => {
60+
should.Throw(
61+
() => {
62+
// @ts-ignore
63+
'foo\\bar'.should.be.posixPath(456);
64+
},
65+
AssertionError,
66+
'expected 456 to be a string'
67+
);
68+
});
69+
70+
it('should throw when both are invalid', () => {
71+
should.Throw(
72+
() => {
73+
const value = 123;
74+
// @ts-ignore
75+
value.should.be.posixPath(456);
76+
},
77+
AssertionError,
78+
'expected 123 to be a string'
79+
);
1080
});
1181
});

0 commit comments

Comments
 (0)