From 82cae6dc582c001dca413b56614a74e7b5c12143 Mon Sep 17 00:00:00 2001 From: kajvans Date: Mon, 12 Feb 2024 19:02:10 +0100 Subject: [PATCH] finally works --- README.md | 1 + dist/{ => cjs}/index.d.ts | 0 dist/{ => cjs}/index.js | 1 - dist/esm/index.d.ts | 5 +++ dist/esm/index.js | 68 +++++++++++++++++++++++++++++++++++++++ dist/esm/package.json | 1 + dist/types/index.d.ts | 5 +++ package.json | 19 +++++++++-- src/index.ts | 3 -- tsconfig.cjs.json | 7 ++++ tsconfig.esm.json | 7 ++++ tsconfig.json | 23 ++----------- tsconfig.types.json | 8 +++++ 13 files changed, 121 insertions(+), 27 deletions(-) rename dist/{ => cjs}/index.d.ts (100%) rename dist/{ => cjs}/index.js (95%) create mode 100644 dist/esm/index.d.ts create mode 100644 dist/esm/index.js create mode 100644 dist/esm/package.json create mode 100644 dist/types/index.d.ts create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.esm.json create mode 100644 tsconfig.types.json diff --git a/README.md b/README.md index c430368..556bc60 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ This library is written because of the problem with jsonwebtokens and cookies. I - Convert timestrings to minutes - Convert timestrings to hours - Convert timestrings to days +- Works in ESM CJS and typescript ## Installation diff --git a/dist/index.d.ts b/dist/cjs/index.d.ts similarity index 100% rename from dist/index.d.ts rename to dist/cjs/index.d.ts diff --git a/dist/index.js b/dist/cjs/index.js similarity index 95% rename from dist/index.js rename to dist/cjs/index.js index e3e5589..db6921d 100644 --- a/dist/index.js +++ b/dist/cjs/index.js @@ -8,7 +8,6 @@ function parseVal(input) { if (!input.includes("d") && !input.includes("h") && !input.includes("m") && !input.includes("s") && !input.includes("ms")) { return "you need to add a value"; } - //order the string so it is in the correct order it must go d,h,m,s,ms so ms, s, m, d, h will return d,h,m,s,ms if (!input.includes("d")) { input = "0d" + input; } diff --git a/dist/esm/index.d.ts b/dist/esm/index.d.ts new file mode 100644 index 0000000..7852981 --- /dev/null +++ b/dist/esm/index.d.ts @@ -0,0 +1,5 @@ +export declare function ToSec(input: string): number; +export declare function ToMin(input: string): number; +export declare function ToHour(input: string): number; +export declare function ToDay(input: string): number; +export declare function ToMs(input: string): number; diff --git a/dist/esm/index.js b/dist/esm/index.js new file mode 100644 index 0000000..0314c25 --- /dev/null +++ b/dist/esm/index.js @@ -0,0 +1,68 @@ +function parseVal(input) { + //check if string contains d,h,m,s and if not containts one of those add them to the string and set the value to 0 and place the value in the correct place + input = input.toLowerCase(); + let data; + if (!input.includes("d") && !input.includes("h") && !input.includes("m") && !input.includes("s") && !input.includes("ms")) { + return "you need to add a value"; + } + if (!input.includes("d")) { + input = "0d" + input; + } + if (!input.includes("h")) { + data = input.split("d"); + input = data[0] + "d0h" + data[1]; + } + if (!input.includes("m")) { + data = input.split("h"); + input = data[0] + "h0m" + data[1]; + } + if (!input.includes("s")) { + data = input.split("m"); + input = data[0] + "m0s" + data[1]; + } + if (!input.includes("ms")) { + data = input.split("s"); + input = data[0] + "s0ms" + data[1]; + } + let returnthis = input; + return returnthis; +} +function splitDate(input) { + input = parseVal(input); + let parts = input.split("d"); + let days = parseInt(parts[0]); + parts = parts[1].split("h"); + let hours = parseInt(parts[0]); + parts = parts[1].split("m"); + let minutes = parseInt(parts[0]); + parts = parts[1].split("s"); + let seconds = parseInt(parts[0]); + parts = parts[1].split("ms"); + let milliseconds = parseInt(parts[0]); + return [days, hours, minutes, seconds, milliseconds]; +} +export function ToSec(input) { + let data = splitDate(input); + let total = data[0] * 86400 + data[1] * 3600 + data[2] * 60 + data[3] + data[4] / 1000; + return total; +} +export function ToMin(input) { + let data = splitDate(input); + let total = data[0] * 1440 + data[1] * 60 + data[2] + data[3] / 60 + data[4] / 60000; + return total; +} +export function ToHour(input) { + let data = splitDate(input); + let total = data[0] * 24 + data[1] + data[2] / 60 + data[3] / 3600 + data[4] / 3600000; + return total; +} +export function ToDay(input) { + let data = splitDate(input); + let total = data[0] + data[1] / 24 + data[2] / 1440 + data[3] / 86400 + data[4] / 86400000; + return total; +} +export function ToMs(input) { + let data = splitDate(input); + let total = data[0] * 86400000 + data[1] * 3600000 + data[2] * 60000 + data[3] * 1000 + data[4]; + return total; +} diff --git a/dist/esm/package.json b/dist/esm/package.json new file mode 100644 index 0000000..1632c2c --- /dev/null +++ b/dist/esm/package.json @@ -0,0 +1 @@ +{"type": "module"} \ No newline at end of file diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts new file mode 100644 index 0000000..7852981 --- /dev/null +++ b/dist/types/index.d.ts @@ -0,0 +1,5 @@ +export declare function ToSec(input: string): number; +export declare function ToMin(input: string): number; +export declare function ToHour(input: string): number; +export declare function ToDay(input: string): number; +export declare function ToMs(input: string): number; diff --git a/package.json b/package.json index 1dc4d71..26ee82a 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,28 @@ { "name": "timestringconverter", - "version": "2.2.0", + "version": "2.2.4", "description": "generetes a number from a time string", - "main": "src/index.ts", + "main": "dist/cjs/index.js", + "module": "dist/esm/index.js", + "files": [ + "dist" + ], "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "node index.js" + "start": "node index.js", + "compile": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json" }, "author": "kaj van schalkwijk", "license": "ISC", "devDependencies": { "ts-node": "^10.9.2" + }, + "exports": { + ".": { + "types": "./dist/types/index.d.ts", + "require": "./dist/cjs/index.js", + "import": "./dist/esm/index.js", + "default": "./dist/esm/index.js" + } } } diff --git a/src/index.ts b/src/index.ts index e2cad09..0dff5fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,9 +7,6 @@ function parseVal(input: string){ return "you need to add a value" } - //order the string so it is in the correct order it must go d,h,m,s,ms so ms, s, m, d, h will return d,h,m,s,ms - - if(!input.includes("d")){ input = "0d" + input; } diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..676f0a4 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./dist/cjs", + "module": "commonjs" + } + } \ No newline at end of file diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..3ca28be --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./dist/esm", + "module": "esnext" + } + } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 1984232..956b515 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,25 +24,6 @@ // "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": "node10", /* 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. */ @@ -105,5 +86,7 @@ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } + }, + "exclude": ["node_modules", "**/*.test.ts"], + "include": ["src/**/*.ts"] } diff --git a/tsconfig.types.json b/tsconfig.types.json new file mode 100644 index 0000000..5fbd3bf --- /dev/null +++ b/tsconfig.types.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./dist/types", + "declaration": true, + "emitDeclarationOnly": true + } + } \ No newline at end of file