Skip to content

Commit

Permalink
feat: convert to ES Modules
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this changes the min Node.js version and changes the
API
  • Loading branch information
coderbyheart committed Nov 15, 2023
1 parent 9d5a1d6 commit 1598d68
Show file tree
Hide file tree
Showing 17 changed files with 16,290 additions and 17,346 deletions.
48 changes: 0 additions & 48 deletions .github/workflows/code-style.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/sync-issue-labels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ on:
branches:
- saga
paths:
- ".github/workflows/sync-issue-labels.yaml"
- '.github/workflows/sync-issue-labels.yaml'
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
- cron: '0 0 * * *'

jobs:
sync_issue_labels:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "18.x"
node-version: '20.x'
- name: Authenticate with NPM registry
run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- name: Keep npm cache around to speed up installs
Expand All @@ -24,6 +24,8 @@ jobs:
key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm ci --no-audit
- name: Format source code with prettier
run: npx prettier -c ./
- name: Lint
run: npx standard
- name: Test
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/update-repo-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:
branches:
- saga
paths:
- "package.json"
- ".github/workflows/update-repo-info.yaml"
- 'package.json'
- '.github/workflows/update-repo-info.yaml'
workflow_dispatch:

jobs:
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"semi": false,
"trailingComma": "none"
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Returns random 8-letter words from the [Webster's Unabridged Dictionary](http://
npm i --save --save-exact @nordicsemiconductor/random-words

```javascript
const { randomWords } = require("@nordicsemiconductor/random-words");
import { randomWords } from '@nordicsemiconductor/random-words'

randomWords().then(console.log); // [ 'scoparin', 'prowling', 'priedieu', 'gantline' ]
console.log(randomWords()) // [ 'scoparin', 'prowling', 'priedieu', 'gantline' ]
```

## CLI Usage
Expand Down
9 changes: 2 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
#!/usr/bin/env node
const { randomWords } = require("./index");
import { randomWords } from './index.js'

const cli = async () => {
const words = await randomWords();
process.stdout.write(words.join(" "));
};

cli();
process.stdout.write(randomWords().join(' '))
1 change: 1 addition & 0 deletions commitlint.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] }
1 change: 0 additions & 1 deletion commitlint.config.js

This file was deleted.

4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { randomWords } = require("@nordicsemiconductor/random-words");
import { randomWords } from '@nordicsemiconductor/random-words'

randomWords().then(console.log);
console.log(randomWords())
4 changes: 1 addition & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* Generate random words
*/
export declare const randomWords: (params?: {
numWords: number;
}) => Promise<string[]>;
export declare const randomWords: (params?: { numWords: number }) => string[]
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const { words, count } = require("./words");
const randomNumber = require("random-number-csprng");
import { words } from './words.js'
import { randomInt } from 'node:crypto'

async function* randomWord() {
/* eslint-disable generator-star-spacing */
/* eslint-disable space-before-function-paren */
function* randomWord() {
while (true) {
const number = await randomNumber(0, count - 1);
yield words[number];
const number = randomInt(0, words.length - 1)
yield words[number]
}
}

module.exports = {
randomWords: async ({ numWords } = { numWords: 4 }) => {
const randomWords = [];
const r = randomWord();
for (let i = 0; i < parseInt(numWords, 10); i++) {
randomWords.push((await r.next()).value);
}
return randomWords;
},
};
export const randomWords = ({ numWords } = { numWords: 4 }) => {
const randomWords = []
const r = randomWord()
for (let i = 0; i < parseInt(numWords, 10); i++) {
randomWords.push(r.next().value)
}
return randomWords
}
Loading

0 comments on commit 1598d68

Please sign in to comment.