diff --git a/.gitignore b/.gitignore index 579431b..7ac71dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Regular folders dist/ node_modules/ +lib # Ignore lock files *-lock.json diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index 496bf60..ed44e52 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,29 @@ "name": "@expressots/boost-ts", "version": "1.2.0", "description": "Expressots: Boost is a collection of libraries for the TypeScript programming language designed to enhance its capabilities across various domains. (@boost-ts)", - "types": "./dist/index.d.ts", + "main": "./lib/cjs/index.js", + "types": "./lib/cjs/types/index.d.ts", + "exports": { + ".": { + "import": { + "types": "./lib/esm/types/index.d.ts", + "default": "./lib/esm/index.mjs" + }, + "require": { + "types": "./lib/cjs/types/index.d.ts", + "default": "./lib/cjs/index.js" + } + } + }, + "files": [ + "lib/**/*" + ], "license": "MIT", - "keywords": [], + "keywords": [ + "pattern-matching", + "text-utils", + "expressots" + ], "repository": { "type": "git", "url": "git+https://github.com/expressots/boost-ts.git" @@ -17,11 +37,20 @@ }, "author": "Richard Zampieri", "scripts": { - "start": "tsnd --respawn --transpile-only --ignore-watch node_modules src/main.ts", - "build": "rimraf dist && tsc -p tsconfig.json", - "build:prod": "npm run build && node dist/main.js", + "prepare": "husky", + "clean": "node scripts/rm.js lib", + "copy": "node scripts/copy.js package.json README.md CHANGELOG.md lib", + "build": "npm run clean && npm run build:cjs && npm run copy", + "build:cjs": "tsc -p tsconfig.cjs.json", + "release": "release-it", "prepublish": "npm run build && npm pack", - "test": "jest" + "publish": "npm publish --tag latest", + "test": "vitest run --reporter default", + "test:watch": "vitest run --watch", + "coverage": "vitest run --coverage", + "format": "prettier --write \"src/**/*.ts\" --cache", + "lint": "eslint \"src/**/*.ts\"", + "lint:fix": "eslint \"src/**/*.ts\" --fix" }, "devDependencies": { "@swc/core": "1.3.37", @@ -29,9 +58,84 @@ "@types/jest": "29.5.0", "jest": "29.5.0", "ts-node-dev": "2.0.0", - "typescript": "5.0.2" + "@codecov/vite-plugin": "0.0.1-beta.6", + "@commitlint/cli": "19.2.1", + "@commitlint/config-conventional": "19.2.2", + "@expressots/core": "2.15.0", + "@release-it/conventional-changelog": "8.0.1", + "@types/express": "4.17.21", + "@types/node": "20.14.10", + "@typescript-eslint/eslint-plugin": "7.16.1", + "@typescript-eslint/parser": "7.16.1", + "@vitest/coverage-v8": "2.0.3", + "eslint": "8.57.0", + "eslint-config-prettier": "9.1.0", + "husky": "9.1.1", + "prettier": "3.3.3", + "release-it": "17.6.0", + "typescript": "5.5.3", + "vite": "5.3.4", + "vite-tsconfig-paths": "4.3.2", + "vitest": "2.0.3" }, "dependencies": { "rimraf": "6.0.1" + }, + "release-it": { + "git": { + "commitMessage": "chore(release): ${version}" + }, + "github": { + "release": true + }, + "npm": { + "publish": false + }, + "plugins": { + "@release-it/conventional-changelog": { + "infile": "CHANGELOG.md", + "preset": { + "name": "conventionalcommits", + "types": [ + { + "type": "feat", + "section": "Features" + }, + { + "type": "fix", + "section": "Bug Fixes" + }, + { + "type": "perf", + "section": "Performance Improvements" + }, + { + "type": "revert", + "section": "Reverts" + }, + { + "type": "docs", + "section": "Documentation" + }, + { + "type": "refactor", + "section": "Code Refactoring" + }, + { + "type": "test", + "section": "Tests" + }, + { + "type": "build", + "section": "Build System" + }, + { + "type": "ci", + "section": "Continuous Integrations" + } + ] + } + } } } +} diff --git a/scripts/copy.js b/scripts/copy.js new file mode 100644 index 0000000..26e3b03 --- /dev/null +++ b/scripts/copy.js @@ -0,0 +1,30 @@ +const fs = require("fs"); +const path = require("path"); + +function copyRecursiveSync(src, dest) { + const exists = fs.existsSync(src); + const stats = exists && fs.statSync(src); + const isDirectory = exists && stats.isDirectory(); + + if (isDirectory) { + fs.mkdirSync(dest); + fs.readdirSync(src).forEach(function (childItemName) { + copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName)); + }); + } else { + fs.copyFileSync(src, dest); + } +} + +if (process.argv.length < 4) { + process.stderr.write("Usage: node copy.js ... \n"); + process.exit(1); +} + +const destination = process.argv[process.argv.length - 1]; + +for (let i = 2; i < process.argv.length - 1; i++) { + const origin = process.argv[i]; + copyRecursiveSync(origin, path.join(destination, path.basename(origin))); + process.stdout.write(`Copied: ${origin} to ${destination}\n`); +} diff --git a/scripts/mv.js b/scripts/mv.js new file mode 100644 index 0000000..be7e307 --- /dev/null +++ b/scripts/mv.js @@ -0,0 +1,28 @@ +const fs = require("fs").promises; + +const moveFile = async (origin, destination) => { + try { + await fs.access(origin); + } catch (error) { + process.stderr.write(`Error: Origin '${origin}' not found\n`); + process.exit(1); + } + + try { + await fs.rename(origin, destination); + process.stdout.write(`Move: ${origin} to ${destination}\n`); + } catch (error) { + process.stderr.write(`Error: Unable to move '${origin}' to '${destination}'\n`); + process.exit(1); + } +}; + +if (process.argv.length !== 4) { + process.stderr.write("Usage: node mv.js \n"); + process.exit(1); +} + +const origin = process.argv[2]; +const destination = process.argv[3]; + +moveFile(origin, destination); diff --git a/scripts/rm.js b/scripts/rm.js new file mode 100644 index 0000000..14f7759 --- /dev/null +++ b/scripts/rm.js @@ -0,0 +1,19 @@ +const fs = require("fs").promises; + +const removeTarget = async (target) => { + try { + await fs.rm(target, { recursive: true, force: true }); + process.stdout.write(`Removed: ${target}\n`); + } catch (error) { + process.stderr.write(`Error: Unable to remove '${target}'\n`); + process.exit(1); + } +}; + +if (process.argv.length !== 3) { + process.stderr.write("Usage: node rm.js \n"); + process.exit(1); +} + +const target = process.argv[2]; +removeTarget(target); diff --git a/tests/match-pattern.test.ts b/tests/match-pattern.test.ts index 640cc20..2ba1eeb 100644 --- a/tests/match-pattern.test.ts +++ b/tests/match-pattern.test.ts @@ -1,48 +1,50 @@ -import { match } from '../src/match-pattern'; +import { match } from "../src/match-pattern"; +import { describe, test, expect } from "vitest"; -describe('testing match pattern function', () => { +describe("testing match pattern function", () => { + const matchValidates = { + 0: () => "this is 0", + 99: () => "this is 99", + "11..=22": () => "this number is in between 11 and 22", + "/abc/": () => "this is a regex", + "2 | 3 | 5 | 8": () => "this number is a prime number", + false: () => "this is a boolean", + "a..=d": () => "this is a char", + "1..=13": () => "Between 1 and 13", + "25 | 50 | 100": () => "A bill", + "/[a-z]/": () => "A lowercase letter", + _: () => "default", + }; - const matchValidates = { - 0: () => "this is 0", - 99: () => "this is 99", - "11..=22": () => "this number is in between 11 and 22", - "/abc/": () => "this is a regex", - "2 | 3 | 5 | 8": () => "this number is a prime number", - false: () => "this is a boolean", - "a..=d": () => "this is a char", - "1..=13": () => "Between 1 and 13", - "25 | 50 | 100": () => "A bill", - "/[a-z]/": () => "A lowercase letter", - "_": () => "default", - }; + const enum Coin { + Penny, + Nickel, + Dime, + Quarter, + } - const enum Coin { - Penny, - Nickel, - Dime, - Quarter - } + test("match pattern - validates", () => { + expect(match(0, matchValidates)).toBe("this is 0"); + expect(match(99, matchValidates)).toBe("this is 99"); + expect(match(11, matchValidates)).toBe("this number is in between 11 and 22"); + expect(match(/abc/, matchValidates)).toBe("this is a regex"); + expect(match(false, matchValidates)).toBe("this is a boolean"); + expect(match("a", matchValidates)).toBe("this is a char"); + expect(match(5, matchValidates)).toBe("this number is a prime number"); + expect(match(10, matchValidates)).toBe("Between 1 and 13"); + expect(match(25, matchValidates)).toBe("A bill"); + expect(match("z", matchValidates)).toBe("A lowercase letter"); + expect(match("1", matchValidates)).toBe("default"); + }); - test('match pattern - validates', () => { - expect(match(0, matchValidates)).toBe("this is 0"); - expect(match(99, matchValidates)).toBe("this is 99"); - expect(match(11, matchValidates)).toBe("this number is in between 11 and 22"); - expect(match(/abc/, matchValidates)).toBe("this is a regex"); - expect(match(false, matchValidates)).toBe("this is a boolean"); - expect(match("a", matchValidates)).toBe("this is a char"); - expect(match(5, matchValidates)).toBe("this number is a prime number"); - expect(match(10, matchValidates)).toBe("Between 1 and 13"); - expect(match(25, matchValidates)).toBe("A bill"); - expect(match("z", matchValidates)).toBe("A lowercase letter"); - expect(match("1", matchValidates)).toBe("default"); - }); - - test('match pattern - enum', () => { - expect(match(Coin.Dime, { - [Coin.Penny]: () => 1, - [Coin.Nickel]: () => 5, - [Coin.Dime]: () => 10, - [Coin.Quarter]: () => 25, - })).toBe(10); - }); -}); \ No newline at end of file + test("match pattern - enum", () => { + expect( + match(Coin.Dime, { + [Coin.Penny]: () => 1, + [Coin.Nickel]: () => 5, + [Coin.Dime]: () => 10, + [Coin.Quarter]: () => 25, + }) + ).toBe(10); + }); +}); diff --git a/tests/optional-pattern.test.ts b/tests/optional-pattern.test.ts index 9c83e9e..25533ac 100644 --- a/tests/optional-pattern.test.ts +++ b/tests/optional-pattern.test.ts @@ -1,21 +1,24 @@ import { Some, None, Optional } from "../src/optional-pattern"; -import { match } from '../src/match-pattern'; +import { match } from "../src/match-pattern"; +import { describe, test, expect } from "vitest"; +describe("testing optional pattern function", () => { + const v1: Optional = Some(1); + const v2: Optional = None(); -describe('testing optional pattern function', () => { - const v1: Optional = Some(1); - const v2: Optional = None(); + test("optional pattern", () => { + expect( + match(v1, { + Some: (x) => x + 1, + None: 0, + }) + ).toBe(2); - test('optional pattern', () => { - expect(match(v1, { - Some: (x) => x + 1, - None: 0, - })).toBe(2); - - expect(match(v2, { - Some: (x) => x + 1, - None: 0, - })).toBe(0); - }) - -}) \ No newline at end of file + expect( + match(v2, { + Some: (x) => x + 1, + None: 0, + }) + ).toBe(0); + }); +}); diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..dc269bb --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "target": "ES2021", + "moduleResolution": "node", + "outDir": "lib/cjs", + "declarationDir": "lib/cjs/types" + } +} diff --git a/tsconfig.json b/tsconfig.json index 3bb7a6e..8cac594 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,118 +1,19 @@ { - "compilerOptions": { - /* Visit https://aka.ms/tsconfig to read more about this file */ - - /* Projects */ - // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ - // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ - // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ - // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ - // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ - // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ - - /* Language and Environment */ - "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ - // "jsx": "preserve", /* Specify what JSX code is generated. */ - "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ - "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ - // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ - // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ - // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ - // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ - // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ - // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ - // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ - - /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ - //"rootDir": "./src", /* Specify the root folder within your source files. */ - "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ - // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ - // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ - // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ - // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ - // "types": [], /* Specify type package names to be included without being referenced in a source file. */ - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ - // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ - // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ - // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ - // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ - // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ - // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ - - /* JavaScript Support */ - // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ - // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ - // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ - - /* Emit */ - "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ - // "declarationMap": true, /* Create sourcemaps for d.ts files. */ - // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ - // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ - // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ - // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ - "outDir": "./dist", /* Specify an output folder for all emitted files. */ - // "removeComments": true, /* Disable emitting comments. */ - // "noEmit": true, /* Disable emitting files from a compilation. */ - // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ - // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ - // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ - // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ - // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ - // "newLine": "crlf", /* Set the newline character for emitting files. */ - // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ - // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ - // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ - // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ - // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ - - /* Interop Constraints */ - // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ - // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ - // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ - "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ - // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ - "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ - - /* Type Checking */ - "strict": true, /* Enable all strict type-checking options. */ - "noImplicitAny": false, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ - // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ - // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ - // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ - // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ - // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ - // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ - // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ - // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ - // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ - // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ - // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ - // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ - // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ - // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ - // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ - // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ - // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ - - /* Completeness */ - // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ - }, - "include": [ - "src/**/*" - ], - "exclude": [ - "node_modules", - "dist", - "src/main.ts", - "test/**/*" - ] + "compilerOptions": { + "strict": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, + "strictNullChecks": false, + "checkJs": true, + "allowJs": true, + "declaration": true, + "noImplicitAny": false, + "allowSyntheticDefaultImports": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "types": ["node", "vitest/globals"] + }, + "include": ["./src/**/*.ts"], + "exclude": ["node_modules", "**/__tests__/*.spec.ts", "scripts/**/*"] }