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

Switch to typescript #178

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# /node_modules/* in the project root is ignored by default
# build artifacts
dist/*
coverage/*
# data definition files
**/*.d.ts
# custom definition files
/src/types/
26 changes: 26 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
parser: '@typescript-eslint/parser',
extends: [
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:jest/recommended',
'prettier',
],
env: {
browser: true,
es6: true,
node: true,
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
ignorePatterns: ['./node_modules/*'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-empty-function': 'off',
'jest/no-try-expect': 'off',
'jest/no-conditional-expect': 'off',
},
};
9 changes: 0 additions & 9 deletions .eslintrc.json

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
dist/
package-lock.json
docs/.vuepress/dist
.eslintcache
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
singleQuote: true,
trailingComma: 'all',
tabWidth: 2,
semi: true,
printWidth: 80,
parser: 'typescript',
};
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Untranspiled build `v8n.esm.browser.js` for modern browsers ([#165](https://github.com/imbrn/v8n/issues/165))

### Fixed

- Bug with schema validation ([#166](https://github.com/imbrn/v8n/pull/166))
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
52 changes: 26 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,18 @@
"files": [
"dist/",
"src/",
"docs/"
"docs/",
"types/"
],
"scripts": {
"test": "jest",
"lint": "eslint ./src",
"lint": "eslint {src,test}/**/*.ts",
"lint:fix": "eslint --fix {src,test}/**/*.ts",
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs",
"build": "rimraf -r dist && rollup -c",
"deploy": "yarn build && npm publish"
},
"husky": {
"hooks": {
"pre-commit": "yarn test && lint-staged"
}
},
"lint-staged": {
"*.{js,md}": [
"prettier --write",
"git add"
]
},
"keywords": [
"javascript",
"validation",
Expand All @@ -46,21 +37,30 @@
"homepage": "https://github.com/imbrn/v8n#readme",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.5",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.7.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.1",
"husky": "^1.0.0-rc.9",
"jest": "^23.1.0",
"lint-staged": "^7.2.0",
"@types/jest": "^26.0.15",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-jest": "^24.1.0",
"husky": ">=4",
"jest": "^26.6.3",
"lint-staged": ">=10",
"prettier": "^1.19.1",
"rimraf": "^2.7.1",
"rollup": "^0.62.0",
"rollup-plugin-babel": "^3.0.5",
"rollup-plugin-uglify": "^4.0.0",
"rollup": "^2.33.1",
"rollup-plugin-typescript2": "^0.29.0",
"ts-jest": "^26.4.4",
"typescript": "^4.0.5",
"vuepress": "^0.12.0"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{ts,json}": "eslint --fix",
"*.{js,ts,css,md}": "prettier --write"
}
}
23 changes: 16 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ function buildConfigBuilder({ name, input, dist = "dist" }) {
format,
transpiled = true,
minified = false,
includeExtension = true,
extension = format,
extension = "",
sourceMap = false
}) => {
function buildFileName() {
return `${name}${includeExtension ? `.${extension}` : ""}${
minified ? ".min" : ""
}.js`;
return `${name}.${format === "es" ? "esm" : format}${
!!extension ? `.${extension}` : ""
}${minified ? ".min" : ""}.js`;
}

function buildPlugins() {
Expand Down Expand Up @@ -60,23 +59,33 @@ const buildConfig = buildConfigBuilder({
});

const configs = [
// AMD″
buildConfig({ format: "amd" }),
// CJS
buildConfig({ format: "cjs" }),
// UMD
buildConfig({ format: "umd" }),
buildConfig({
format: "umd",
minified: true,
includeExtension: false,
sourceMap: true
}),
// IIFE
buildConfig({ format: "iife", extension: "browser" }),
buildConfig({
format: "iife",
extension: "browser",
minified: true,
sourceMap: true
}),
buildConfig({ format: "esm" }),
// ESM
buildConfig({ format: "es" }),
buildConfig({
format: "es",
extension: "browser",
transpiled: false
}),
// System
buildConfig({ format: "system" })
];

Expand Down
46 changes: 28 additions & 18 deletions src/Context.js → src/Context.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
import Rule from "./Rule";
import Modifier from "./Modifier";
import ValidationError from "./ValidationError";
import Rule from './Rule';
import Modifier from './Modifier';
import ValidationError from './ValidationError';
import { ModifierDefinition, RuleFunction } from '../types/v8n';

class Context {
constructor(chain = [], nextRuleModifiers = []) {
[key: string]: any;
chain: Rule[];
nextRuleModifiers: Modifier[];

constructor(chain: Rule[] = [], nextRuleModifiers: Modifier[] = []) {
this.chain = chain;
this.nextRuleModifiers = nextRuleModifiers;
}

_applyRule(ruleFn, name) {
return (...args) => {
_applyRule(ruleFn: RuleFunction, name: string) {
return (...args: any[]): Context => {
this.chain.push(
new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers)
new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),
);
this.nextRuleModifiers = [];
return this;
};
}

_applyModifier(modifier, name) {
_applyModifier(modifier: ModifierDefinition, name: string): Context {
this.nextRuleModifiers.push(
new Modifier(name, modifier.simple, modifier.async)
new Modifier(name, modifier.simple, modifier.async),
);
return this;
}

_clone() {
_clone(): Context {
return new Context(this.chain.slice(), this.nextRuleModifiers.slice());
}

test(value) {
test(value?: unknown): boolean {
return this.chain.every(rule => rule._test(value));
}

testAll(value) {
const err = [];
testAll(value?: unknown): ValidationError[] {
const err: ValidationError[] = [];
this.chain.forEach(rule => {
try {
rule._check(value);
Expand All @@ -45,7 +50,7 @@ class Context {
return err;
}

check(value) {
check(value?: unknown): void {
this.chain.forEach(rule => {
try {
rule._check(value);
Expand All @@ -55,23 +60,28 @@ class Context {
});
}

testAsync(value) {
testAsync(value?: unknown): Promise<unknown | ValidationError> {
return new Promise((resolve, reject) => {
executeAsyncRules(value, this.chain.slice(), resolve, reject);
});
}
}

function executeAsyncRules(value, rules, resolve, reject) {
function executeAsyncRules(
value: any,
rules: Rule[],
resolve: (value?: unknown) => void,
reject: (reason?: any) => void,
) {
if (rules.length) {
const rule = rules.shift();
const rule = rules.shift()!;
rule._testAsync(value).then(
() => {
executeAsyncRules(value, rules, resolve, reject);
},
cause => {
reject(new ValidationError(rule, value, cause));
}
},
);
} else {
resolve(value);
Expand Down
9 changes: 0 additions & 9 deletions src/Modifier.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/Modifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AsyncModifierFunction, SimpleModifierFunction } from '../types/v8n';

class Modifier {
name: string;
perform: SimpleModifierFunction;
performAsync: AsyncModifierFunction;

constructor(
name: string,
perform: SimpleModifierFunction,
performAsync: AsyncModifierFunction,
) {
this.name = name;
this.perform = perform;
this.performAsync = performAsync;
}
}

export default Modifier;