Skip to content

Commit

Permalink
feat(core): migrate code to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
SMAKSS committed Nov 3, 2023
1 parent a584bcb commit cd2f144
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 134 deletions.
49 changes: 39 additions & 10 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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}}
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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).
113 changes: 0 additions & 113 deletions index.cjs

This file was deleted.

3 changes: 0 additions & 3 deletions index.mjs

This file was deleted.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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"
}
107 changes: 107 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/**/*"]
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -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==

0 comments on commit cd2f144

Please sign in to comment.