Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init #1

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
DS_Store
8 changes: 8 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('mocha').MochaOptions} */
// process.env.TS_NODE_PROJECT = 'tsconfig.json';
module.exports = {
require: 'ts-node/register',
spec: ['tests/**/*.test.ts'],
'watch-files': ['src/**/*.ts', 'tests/**/*.ts'],
timeout: 10000
};
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests
src
.github
.vscode
node_modules
tsconfig.json
tsup.config.ts
.mocharc.js
README.md
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@operationspark.org/ts-assertion:registry=https://npm.pkg.github.com
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["tsup"]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Operation Spark

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
115 changes: 115 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,121 @@ This library provides assertions for testing types

## Installation

```sh
npm install -D @operationspark.org/ts-assertion
```

## Usage

### `CodeCheckerOptions`

| Name | Description | |
| ------------- | ---------------------------------- | ---------- |
| `pathname` | Pathname to input file | `Optional` |
| `globalTypes` | string of globally available types | `Optional` |
| `globalPaths` | string of globally available paths | `Optional` |

### `CodeChecker` Methods

| Name | Description | Returns |
| -------- | -------------------------------------------------------------- | ------- |
| `test` | Test the code string and return a boolean if the code is valid | boolean |
| `assert` | Test the code string. Chain `isValid()` or `isNotValid()` | void |

### `CodeChecker` global config

Set global configurations for all instances of `CodeChecker`

```ts
import { CodeChecker } from '@operationspark.org/ts-assertion';

CodeChecker.config.setGlobalPaths([
'path/to/file.ts',
'path/to/other/file.d.ts'
]); // default is []
CodeChecker.config.setVerbose(true); // default: false
```

### Basic Usage (No Options)

```ts
import { CodeChecker } from '@operationspark.org/ts-assertion';

const checker = new CodeChecker();

// Test returns boolean. `true` if the code is valid
checker.test('const str: string = "test";'); // true
checker.test('const str: string = 1;'); // false

// Assert throws an error if the code is not valid
checker.assert('const str: string = "test";').isValid();

// Assert throws an error if the code is valid
checker.assert('const str: string = 1;').isNotValid();
```

### Advanced Usage (With Options)

> `path/to/file.ts`
>
> ```ts
> export type StringType = string;
> export type NumberType = number;
> export type BooleanType = boolean;
> ```

```ts
import { CodeChecker } from '@operationspark.org/ts-assertion';

const options: = {
pathname: 'path/to/file.ts',
globalTypes: 'type PrimitiveType = string | number | boolean;',
globalPaths: ['path/to/file.ts', 'path/to/other/file.d.ts'],
};
type TypeNames = 'StringType' | 'NumberType' | 'BooleanType';
const checker = new CodeChecker<TypeNames>(options);


// Test returns boolean. `true` if the code is valid
checker.test('const str: StringType = "test";') // true
checker.test('const str: string = 1;') // false

// Assert throws an error if the code is not valid/invalid
checker.assert('const str: StringType = "test";').isValid();
checker.assert('const str: string = 1;').isNotValid();

// Or more specifically, just test the type in the file
checker.test(
'const str: StringType = "test";',
'StringType'
) // true
checker.assert(
'const str: StringType = "test";', 'StringType'
).isValid();
```

## Development

### Setup

```sh
npm install
```

### Test

```sh
npm test
```

#### Watch

```sh
npm run test:dev
```

### Publish

```sh
npm run publish:npm
```
Loading