Skip to content

Commit

Permalink
convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed May 23, 2024
1 parent 381e7b7 commit 77f7f60
Show file tree
Hide file tree
Showing 15 changed files with 3,028 additions and 507 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
root: true
extends: silverwind
extends:
- silverwind
- silverwind-typescript
13 changes: 6 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [16, 18, 20]

runs-on: ubuntu-latest
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{matrix.node}}
- run: make test
node-version: latest
- run: make lint test
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/.vscode
/dist
/node_modules
/npm-debug.log*
/yarn-error.log
/yarn.lock
28 changes: 23 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SOURCE_FILES := index.ts
DIST_FILES := dist/index.js

node_modules: package-lock.json
npm install --no-save
@touch node_modules
Expand All @@ -7,12 +10,27 @@ deps: node_modules

.PHONY: lint
lint: node_modules
npx eslint --color .
npx eslint --ext js,jsx,ts,tsx --color .
npx tsc

.PHONY: lint-fix
lint-fix: node_modules
npx eslint --ext js,jsx,ts,tsx --color . --fix
npx tsc

.PHONY: test
test: node_modules
npx vitest
npx tsd

.PHONY: test-update
test-update: node_modules
npx vitest -u

.PHONY: build
build: node_modules $(DIST_FILES)

$(DIST_FILES): $(SOURCE_FILES) package-lock.json vite.config.ts
npx vite build

.PHONY: publish
publish: node_modules
Expand All @@ -27,16 +45,16 @@ update: node_modules
@touch node_modules

.PHONY: path
patch: node_modules lint test
patch: node_modules lint test build
npx versions patch package.json package-lock.json
@$(MAKE) --no-print-directory publish

.PHONY: minor
minor: node_modules lint test
minor: node_modules lint test build
npx versions minor package.json package-lock.json
@$(MAKE) --no-print-directory publish

.PHONY: major
major: node_modules lint test
major: node_modules lint test build
npx versions major package.json package-lock.json
@$(MAKE) --no-print-directory publish
49 changes: 0 additions & 49 deletions index.d.ts

This file was deleted.

15 changes: 0 additions & 15 deletions index.js

This file was deleted.

13 changes: 0 additions & 13 deletions index.test-d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion index.test.js → index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cidrRegex, {v4, v6} from "./index.js";
import cidrRegex, {v4, v6} from "./index.ts";

const v4positive = [
"0.0.0.0/16",
Expand Down
24 changes: 24 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ipRegex from "ip-regex";

type Options = {
/**
Only match an exact string. Useful with `RegExp#test()` to check if a string is an IP address. *(`false` matches any IP address in a string)*
@default false
*/
readonly exact?: boolean;
}

const defaultOpts: Options = {exact: false};
const v4str = `${ipRegex.v4().source}\\/(3[0-2]|[12]?[0-9])`;
const v6str = `${ipRegex.v6().source}\\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])`;

// pre-compile only the exact regexes as global flag makes regex objects stateful
const v4exact = new RegExp(`^${v4str}$`);
const v6exact = new RegExp(`^${v6str}$`);
const v46exact = new RegExp(`(?:^${v4str}$)|(?:^${v6str}$)`);

const cidrRegex = ({exact}: Options = defaultOpts) => exact ? v46exact : new RegExp(`(?:${v4str})|(?:${v6str})`, "g");
export const v4 = cidrRegex.v4 = ({exact}: Options = defaultOpts) => exact ? v4exact : new RegExp(v4str, "g");
export const v6 = cidrRegex.v6 = ({exact}: Options = defaultOpts) => exact ? v6exact : new RegExp(v6str, "g");
export default cidrRegex;
Loading

0 comments on commit 77f7f60

Please sign in to comment.