Skip to content

Commit

Permalink
feat(core): migrate to typescript
Browse files Browse the repository at this point in the history
Also, enhance dx tools
  • Loading branch information
SMAKSS committed Nov 4, 2023
1 parent 295808f commit 8b1706b
Show file tree
Hide file tree
Showing 23 changed files with 2,771 additions and 85 deletions.
11 changes: 11 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Build files
dist

#prettier config
prettier.config.cjs

# lint-staged config
lint-staged.config.cjs

# commitlint config
commitlint.config.cjs
36 changes: 36 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"env": {
"browser": true,
"es6": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
// Specifying Parser
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"tsconfigRootDir": ".",
"project": ["./tsconfig.json"]
},
// Configuring third-party plugins
"plugins": ["@typescript-eslint"],
// Resolve imports
"rules": {
"linebreak-style": "off",
// Disallow the `any` type.
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true,
"types": {
"{}": false
}
}
],
"react-hooks/exhaustive-deps": "off",
// Enforce the use of the shorthand syntax.
"object-shorthand": "error",
"no-console": "warn"
}
}
42 changes: 42 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
Thank you for contributing by opening an issue! Please take a moment to review our Code of Conduct (found in the CODE_OF_CONDUCT.md file) to keep the community approachable and respectful.
For bug reports, please fill out the information below to help us understand the issue and address it in a timely manner. We value your feedback and contributions.
-->

# Brief Description
<!-- A brief description of the issue.-->

## Environment

- `@smakss/random-array-element` version:
- Framework version (`react`, `react-native`, `Next.js`, `Remix` etc.`):
- `node.js` version:
- `npm` (or `yarn`) version:

## Code Snippet
<!-- Please provide any relevant code snippets or examples here -->

```javascript
// Your code here
```

## Steps to Reproduce
<!-- What steps did we need to take to encounter the problem? -->

## Expected Behavior
<!-- What you expected to happen -->

## Actual Behavior
<!-- What actually happened. Include the full error message/screenshots/anything that might help understanding the issue -->

## Reproduction Repository
<!--
If possible, please provide a repository link where the issue can be reproduced. A minimal test case would be greatly appreciated and can significantly speed up the resolution process.
-->

## Problem Description
<!-- A clear and concise description of what the problem is. Include any additional context that might help us understand the issue. -->

## Suggested Solution
<!-- If you have any suggestion on how to fix the issue please provide it here. If not, just leave this section blank. -->
16 changes: 11 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
reviewers:
- "SMAKSS"
assignees:
- "SMAKSS"
labels:
- "dependencies"
open-pull-requests-limit: 5
63 changes: 63 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: PR Checks

on:
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Print head and base refs
run: |
echo "Head ref: ${{ github.head_ref }}"
echo "Base ref: ${{ github.base_ref }}"
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "18"

- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
**/node_modules
key: ${{ runner.os }}-node-18-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-18-yarn-
- name: Install dependencies
run: yarn install

- name: Type check
run: yarn typecheck

- name: Check code quality
run: |
yarn lint
yarn format:check
yarn run --if-present generate
- name: Security Audit
run: yarn audit --level moderate || true
continue-on-error: true

- name: Security Audit Summary
if: success() || failure()
run: yarn audit --summary

- name: Archive production artifacts
uses: actions/upload-artifact@v3
with:
name: build-artifacts
path: ./build/

- name: Error handling
if: failure()
run: echo "CI failed. Please check the logs."
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/random-array-element"
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}}
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Logs
logs
*.log
yarn-debug.log*
yarn-error.log*

# Dependency directories
node_modules

# Generate output
dist

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn commitlint --edit ${1}
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
11 changes: 11 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package.json
.cache
.shadowenv.d
.vscode
build
dist
node_modules
public
.github
tmp
*.yml
File renamed without changes.
File renamed without changes.
64 changes: 28 additions & 36 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,65 @@
# Random array element without repetition
# Random Array Element without Repetition

![npm](https://img.shields.io/npm/v/@smakss/random-array-element) ![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@smakss/random-array-element) ![NPM](https://img.shields.io/npm/l/@smakss/random-array-element) ![npm](https://img.shields.io/npm/dt/@smakss/random-array-element) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/random-array-element)
![npm](https://img.shields.io/npm/v/@smakss/random-array-element) ![NPM](https://img.shields.io/npm/l/@smakss/random-array-element) ![npm](https://img.shields.io/npm/dt/@smakss/random-array-element) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/random-array-element)

In order to select an element within an array, you can simply use `Math.random()` but what about when you don't want to select an element that selected before. Here this package will come to use. This package works with simple closure, so you can simply invoke randomArrayElement once everywhere in your project then use it to generate random elements from your array no matter your array is flat or nested, it will choose a random unique element from your array until all the array element get exhausted.
Selecting a random element from an array is simple with `Math.random()`. However, if you need to ensure that each element is only selected once until all elements have been chosen, `@smakss/random-array-element` is the ideal solution. Utilizing closures, this package allows you to initialize a function once and then repeatedly obtain unique, randomly-selected elements from your array, without repeats until the array is exhausted.

## Demo

You can check the [working demo](https://runkit.com/smakss/random-array-element) in runkit.
You can check the [working demo](https://runkit.com/smakss/random-array-element) on RunKit.

or

[![View @smakss/random-array-element](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/smakss-random-array-element-7yizos?fontsize=14&hidenavigation=1&theme=dark)

## How it works?
## Installation

To install it you can simply do the following command:
Install the package using npm or Yarn:

```bash
npm i @smakss/random-array-element
or
# or
yarn add @smakss/random-array-element
```

to include it with common js module you should do this:
## Usage

To include it with CommonJS module you can do this:

```js
var randomArrayElement = require("@smakss/random-array-element");
const randomArrayElement = require('@smakss/random-array-element');
```

and to include it with ECMAscript module you can simply do this one:
For ECMAScript modules:

```js
import randomArrayElement from "@smakss/random-array-element";
import randomArrayElement from '@smakss/random-array-element';
```

then to use it within your application you can do it just like this:
Example usage:

```js
const chooser = randomArrayElement();

// Result: -1
// If the input was not array or was empty the chooser will be equal to -1, so chooser() will throw an error.
// Initialize once for an array
const getRandomElement = randomArrayElement(['apple', 'banana', 'cherry']);

console.log(getRandomElement()); // Result: 'banana' (example output)
console.log(getRandomElement()); // Result: 'apple' (example output)
// ...after all items have been returned, it resets.
console.log(getRandomElement()); // Result: 'cherry' (example output)
// ...continues to return a new random item.
```

```js
const chooser = randomArrayElement(["Foo", "Bar", "FU", "FooBar"]);
// Initiating the selector once and then we can use the returned function to select random none repeatable elements from the array.

chooser();
// Result: Foo
If an empty or non-array input is passed, the function will return -1, indicating no selection can be made:

chooser();
// Result: Bar

chooser();
// Result: FU

chooser();
// Result: FooBar

chooser();
// Result: Foo
// It will only repeats, once all items within the array are exhausted.
```js
const getRandomElement = randomArrayElement([]);
console.log(getRandomElement()); // Result: -1
```

## Contributing

Interested in making contributions to this project? Please see [CONTRIBUTING.md](https://github.com/SMAKSS/random-array-element/blob/master/.github/CONTRIBUTING.md) for guidelines and details.
Contributions are welcome! 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/random-array-element/blob/master/.github/CODE_OF_CONDUCT.md).
To ensure a welcoming and safe community, our [Code of Conduct](./CODE_OF_CONDUCT.md) outlines expected behaviors for all participants.
Loading

0 comments on commit 8b1706b

Please sign in to comment.