From cd2f144ad8c1d2cad1775c137bc71c49b00b84c1 Mon Sep 17 00:00:00 2001 From: MAKSS Date: Sat, 4 Nov 2023 00:47:26 +0330 Subject: [PATCH] feat(core): migrate code to typescript --- .github/workflows/npm-publish.yml | 49 ++++++++++--- Readme.md | 6 +- index.cjs | 113 ------------------------------ index.mjs | 3 - package.json | 13 ++-- src/index.ts | 107 ++++++++++++++++++++++++++++ tsconfig.json | 16 +++++ yarn.lock | 8 +++ 8 files changed, 181 insertions(+), 134 deletions(-) delete mode 100644 index.cjs delete mode 100644 index.mjs create mode 100644 src/index.ts create mode 100644 tsconfig.json create mode 100644 yarn.lock diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index a480b4f..8b4742e 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -1,7 +1,4 @@ -# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created -# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages - -name: "@smakss/password-generator" +name: "@smakss/react-scroll-direction" on: release: @@ -14,32 +11,64 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: - node-version: 16 + node-version: 18 + - run: yarn + - run: yarn generate + - uses: actions/upload-artifact@v3 + with: + name: built-package + path: | + ./path-to-your-build-folder + package.json publish-npm: needs: build runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - uses: actions/download-artifact@v3 + with: + name: built-package - uses: actions/setup-node@v3 with: - node-version: 16 + node-version: 18 registry-url: https://registry.npmjs.org/ scope: "@smakss" - - run: npm publish + - run: yarn install + - run: yarn generate + - run: | + if grep -q "beta" package.json; then + echo "Publishing Beta to npm" + npm publish --tag beta + else + echo "Publishing Release to npm" + npm publish + fi env: - NODE_AUTH_TOKEN: ${{secrets.npm_token}} + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} publish-gpr: needs: build runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - uses: actions/download-artifact@v3 + with: + name: built-package - uses: actions/setup-node@v3 with: - node-version: 16 + node-version: 18 registry-url: https://npm.pkg.github.com/ scope: "@smakss" - - run: npm publish + - run: yarn install + - run: yarn generate + - run: | + if grep -q "beta" package.json; then + echo "Publishing Beta to GitHub Package Registry" + npm publish --tag beta + else + echo "Publishing Release to GitHub Package Registry" + npm publish + fi env: NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/Readme.md b/Readme.md index cd2ab51..e55ef2d 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,6 @@ # Password Generator -![npm](https://img.shields.io/npm/v/@smakss/password-generator) ![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@smakss/password-generator) ![NPM](https://img.shields.io/npm/l/@smakss/password-generator) ![npm](https://img.shields.io/npm/dt/@smakss/password-generator) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/password-generator) +![npm](https://img.shields.io/npm/v/@smakss/password-generator) ![NPM](https://img.shields.io/npm/l/@smakss/password-generator) ![npm](https://img.shields.io/npm/dt/@smakss/password-generator) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/password-generator) Secure passwords for users might be a big challenge for us. This package will help you to generate secure random password easily. You can choose allowed characters and even defined them by yourself with random or given length. Also, this package uses ES6+ syntax so if you using older standards for writing JS code you may need a transpiler for it. @@ -83,8 +83,8 @@ PasswordGenerator({ length: 10, characters: ["a", 1, "~"] }); ## Contributing -Interested in making contributions to this project? Please see [CONTRIBUTING.md](https://github.com/SMAKSS/password-generator/blob/master/.github/CONTRIBUTING.md) for guidelines and details. +Interested in making contributions to this project? Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines and details. ## Code of Conduct -We value and prioritize the well-being of all our contributors and users. To ensure that this project remains a welcoming space for everyone, please refer to our [Code of Conduct](https://github.com/SMAKSS/password-generator/blob/master/.github/CODE_OF_CONDUCT.md). +We value and prioritize the well-being of all our contributors and users. To ensure that this project remains a welcoming space for everyone, please refer to our [Code of Conduct](./CODE_OF_CONDUCT.md). diff --git a/index.cjs b/index.cjs deleted file mode 100644 index 586d861..0000000 --- a/index.cjs +++ /dev/null @@ -1,113 +0,0 @@ -'use strict'; -const caps = [ - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - 'G', - 'H', - 'I', - 'J', - 'K', - 'L', - 'M', - 'N', - 'O', - 'P', - 'Q', - 'R', - 'S', - 'T', - 'U', - 'V', - 'W', - 'X', - 'Y', - 'Z' -]; -const lower = [ - 'a', - 'b', - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i', - 'j', - 'k', - 'l', - 'm', - 'n', - 'o', - 'p', - 'q', - 'r', - 's', - 't', - 'u', - 'v', - 'w', - 'x', - 'y', - 'z' -]; -const spec = [ - '@', - '%', - '+', - '', - '/', - "'", - '!', - '#', - '$', - '^', - '?', - ':', - ',', - ')', - '(', - '}', - '{', - ']', - '[', - '~', - '-', - '_', - '.' -]; -const num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - -function PasswordGenerator({ - length = 0, - lowerIncluded = true, - capsIncluded = true, - numIncluded = true, - specIncluded = true, - characters = [] -}) { - if (!length) length = Math.ceil(Math.random() * 10 * 2); - let password = ''; - const allowedCharacters = - characters.length > 0 - ? characters - : [ - ...(lowerIncluded ? lower : []), - ...(numIncluded ? num : []), - ...(capsIncluded ? caps : []), - ...(specIncluded ? spec : []) - ]; - const allowedCharactersLength = allowedCharacters.length; - for (let i = 0; i < length; i++) { - password += - allowedCharacters[Math.floor(Math.random() * allowedCharactersLength)]; - } - - return password; -} - -module.exports = PasswordGenerator; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8fcefa1..0000000 --- a/index.mjs +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; -import PasswordGenerator from './index.cjs'; -export default PasswordGenerator; diff --git a/package.json b/package.json index d65fdd9..b6bcc1d 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,8 @@ "url": "https://github.com/SMAKSS/password-generator/issues" }, "description": "Generate random password.", - "exports": { - "import": "./index.mjs", - "require": "./index.cjs" + "devDependencies": { + "typescript": "^5.2.2" }, "homepage": "https://github.com/SMAKSS/password-generator#readme", "keywords": [ @@ -20,12 +19,16 @@ "Random-Password-Generator" ], "license": "MIT", - "main": "./index.cjs", + "main": "dist/index.js", "name": "@smakss/password-generator", "repository": { "type": "git", "url": "git+https://github.com/SMAKSS/password-generator.git" }, + "scripts": { + "generate": "tsc -p ." + }, "type": "module", - "version": "1.1.4" + "types": "lib", + "version": "2.0.0-beta.0" } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..29f65bc --- /dev/null +++ b/src/index.ts @@ -0,0 +1,107 @@ +/** Array of numeric characters */ +const num: string[] = "0123456789".split(""); + +/** Array of uppercase alphabetic characters */ +const caps: string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); + +/** Array of lowercase alphabetic characters */ +const lower: string[] = "abcdefghijklmnopqrstuvwxyz".split(""); + +/** Array of special characters */ +const spec: string[] = [ + "@", + "%", + "+", + "\\", + "/", + "'", + "!", + "#", + "$", + "^", + "?", + ":", + ",", + ")", + "(", + "}", + "{", + "]", + "[", + "~", + "-", + "_", + ".", +]; + +/** + * Options for the PasswordGenerator function. + * @interface + */ +interface PasswordGeneratorOptions { + length?: number; + lowerIncluded?: boolean; + capsIncluded?: boolean; + numIncluded?: boolean; + specIncluded?: boolean; + characters?: (string | number)[]; +} + +/** + * Generates a random index within a given range. + * @param max The upper bound of the random index (exclusive). + * @returns A random integer between 0 and max. + */ +function getRandomIndex(max: number): number { + return Math.floor(Math.random() * max); +} + +/** + * Generates a random password based on the provided options. + * @param options Configuration options for generating the password. + * @param options.length The length of the password. + * @param options.lowerIncluded Whether to include lowercase characters. + * @param options.capsIncluded Whether to include uppercase characters. + * @param options.numIncluded Whether to include numeric characters. + * @param options.specIncluded Whether to include special characters. + * @param options.characters A custom array of characters to be used for generating the password. + * @returns A string representing the generated password. + * + * @example + * // Generates a 12-character password with all options enabled + * const password = PasswordGenerator({ length: 12 }); + * console.log(password); + * + * @example + * // Generates a password using a custom set of characters + * const customChars = 'abcdef'.split(''); + * const password = PasswordGenerator({ characters: customChars }); + * console.log(password); + */ +function PasswordGenerator({ + length = Math.ceil(Math.random() * 20), + lowerIncluded = true, + capsIncluded = true, + numIncluded = true, + specIncluded = true, + characters = [], +}: PasswordGeneratorOptions = {}): string { + const allowedCharacters = + characters.length > 0 + ? characters + : [ + ...(lowerIncluded ? lower : []), + ...(capsIncluded ? caps : []), + ...(numIncluded ? num : []), + ...(specIncluded ? spec : []), + ]; + + const password = Array.from( + { length }, + () => allowedCharacters[getRandomIndex(allowedCharacters.length)] + ).join(""); + + return password; +} + +export default PasswordGenerator; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..73ba537 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "esModuleInterop": true, + "module": "commonjs", + "moduleResolution": "node", + "noImplicitReturns": true, + "noUnusedLocals": true, + "outDir": "dist", + "sourceMap": true, + "strict": true, + "target": "es2020" + }, + "include": ["src/**/*"] +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..cd2bf06 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +typescript@^5.2.2: + version "5.2.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78" + integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==