Skip to content

Commit

Permalink
Update dev practices
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Mar 11, 2021
1 parent 3ba6834 commit 1277ad4
Show file tree
Hide file tree
Showing 11 changed files with 870 additions and 1,413 deletions.
6 changes: 3 additions & 3 deletions async/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @param size Size of the ID. The default size is 21.
* @returns A promise with a random string.
*/
export function nanoid (size?: number): Promise<string>
export function nanoid(size?: number): Promise<string>

/**
* A low-level function.
Expand All @@ -35,7 +35,7 @@ export function nanoid (size?: number): Promise<string>
* })
* ```
*/
export function customAlphabet (
export function customAlphabet(
alphabet: string,
size: number
): () => Promise<string>
Expand All @@ -53,4 +53,4 @@ export function customAlphabet (
* @param bytes Size of the array.
* @returns A promise with a random bytes array.
*/
export function random (bytes: number): Promise<Uint8Array>
export function random(bytes: number): Promise<Uint8Array>
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @param size Size of the ID. The default size is 21.
* @returns A random string.
*/
export function nanoid (size?: number): string
export function nanoid(size?: number): string

/**
* Generate secure unique ID with custom alphabet.
Expand All @@ -30,7 +30,7 @@ export function nanoid (size?: number): string
* nanoid() //=> "8ё56а"
* ```
*/
export function customAlphabet (alphabet: string, size: number): () => string
export function customAlphabet(alphabet: string, size: number): () => string

/**
* Generate unique ID with custom random generator and alphabet.
Expand All @@ -57,7 +57,7 @@ export function customAlphabet (alphabet: string, size: number): () => string
* @param random A random bytes generator.
* @returns A random string generator.
*/
export function customRandom (
export function customRandom(
alphabet: string,
size: number,
random: (bytes: number) => Uint8Array
Expand Down Expand Up @@ -85,4 +85,4 @@ export const urlAlphabet: string
* @param bytes Size of the array.
* @returns An array of random bytes.
*/
export function random (bytes: number): Uint8Array
export function random(bytes: number): Uint8Array
4 changes: 2 additions & 2 deletions non-secure/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @param size Size of the ID. The default size is 21.
* @returns A random string.
*/
export function nanoid (size?: number): string
export function nanoid(size?: number): string

/**
* Generate URL-friendly unique ID based on the custom alphabet.
Expand All @@ -27,4 +27,4 @@ export function nanoid (size?: number): string
* model.id = //=> "8ё56а"
* ```
*/
export function customAlphabet (alphabet: string, size: number): () => string
export function customAlphabet(alphabet: string, size: number): () => string
52 changes: 34 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"sideEffects": false,
"types": "./index.d.ts",
"devDependencies": {
"@babel/core": "^7.13.8",
"@logux/eslint-config": "^44.1.2",
"@logux/sharec-config": "^0.8.5",
"@babel/core": "^7.13.10",
"@logux/eslint-config": "^45.2.0",
"@logux/sharec-config": "^0.9.0",
"@parcel/optimizer-cssnano": "2.0.0-nightly.612",
"@parcel/optimizer-htmlnano": "2.0.0-nightly.612",
"@parcel/packager-css": "2.0.0-nightly.612",
Expand All @@ -40,37 +40,37 @@
"@parcel/transformer-html": "2.0.0-nightly.612",
"@parcel/transformer-postcss": "2.0.0-nightly.612",
"@parcel/transformer-posthtml": "2.0.0-nightly.612",
"@size-limit/dual-publish": "^4.9.2",
"@size-limit/preset-small-lib": "^4.9.2",
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"@size-limit/dual-publish": "^4.10.1",
"@size-limit/preset-small-lib": "^4.10.1",
"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"benchmark": "^2.1.4",
"check-dts": "^0.4.4",
"colorette": "^1.2.2",
"cuid": "^2.1.8",
"dual-publish": "^1.0.3",
"dual-publish": "^1.0.4",
"eslint": "^7.21.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-es5": "^1.5.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.5",
"eslint-plugin-jest": "^24.2.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prefer-let": "^1.1.0",
"eslint-plugin-prettierx": "^0.17.1",
"eslint-plugin-promise": "^4.3.1",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-unicorn": "^28.0.2",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"parcel": "^2.0.0-nightly.477",
"postcss": "^8.2.6",
"postcss": "^8.2.8",
"prettier": "^2.2.1",
"rndm": "^1.2.0",
"secure-random-string": "^1.1.3",
"shortid": "^2.2.16",
"simple-pre-commit": "^1.2.1",
"size-limit": "^4.9.2",
"simple-git-hooks": "^2.0.2",
"size-limit": "^4.10.1",
"terser": "^5.6.0",
"typescript": "^4.2.2",
"typescript": "^4.2.3",
"uid-safe": "^2.1.5",
"uuid": "^8.3.2",
"yaspeller": "^7.0.0"
Expand Down Expand Up @@ -141,10 +141,26 @@
"lint-staged": {
"index.browser.js": "test/update-prebuild.js",
"*.md": "yaspeller",
"*.js": "eslint --fix",
"*.ts": "eslint --fix"
"*.js": [
"prettier --write",
"eslint --fix"
],
"*.ts": [
"prettier --write",
"eslint --fix"
]
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
},
"prettier": {
"arrowParens": "avoid",
"jsxSingleQuote": false,
"quoteProps": "consistent",
"semi": false,
"singleQuote": true,
"trailingComma": "none"
},
"simple-pre-commit": "npx lint-staged",
"jest": {
"testEnvironment": "node",
"coverageThreshold": {
Expand Down Expand Up @@ -200,6 +216,6 @@
},
"sharec": {
"config": "@logux/sharec-config",
"version": "0.8.5"
"version": "0.9.0"
}
}
6 changes: 3 additions & 3 deletions test/async.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let crypto = require('crypto')

global.crypto = {
getRandomValues (array) {
getRandomValues(array) {
for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256)
}
Expand All @@ -13,7 +13,7 @@ let { urlAlphabet } = require('..')
let browser = require('../async/index.browser.js')
let node = require('../async/index.js')

function times (size, callback) {
function times(size, callback) {
let array = []
for (let i = 0; i < size; i++) {
array.push(1)
Expand All @@ -26,7 +26,7 @@ for (let type of ['node', 'browser']) {
let { nanoid, customAlphabet, random } = type === 'node' ? node : browser

describe('nanoid', () => {
function mock (callback) {
function mock(callback) {
crypto.randomFill = callback
jest.resetModules()
nanoid = require('../async').nanoid
Expand Down
10 changes: 5 additions & 5 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let suite = new benchmark.Suite()
let nanoid2 = customAlphabet('1234567890abcdef-', 10)
let asyncNanoid2 = aCustomAlphabet('1234567890abcdef-', 10)

function formatNumber (number) {
function formatNumber(number) {
return String(number)
.replace(/\d{3}$/, ',$&')
.replace(/^(\d)(\d{3})/, '$1,$2')
Expand Down Expand Up @@ -48,31 +48,31 @@ suite
})
.add('async nanoid', {
defer: true,
fn (defer) {
fn(defer) {
aNanoid().then(() => {
defer.resolve()
})
}
})
.add('async customAlphabet', {
defer: true,
fn (defer) {
fn(defer) {
asyncNanoid2().then(() => {
defer.resolve()
})
}
})
.add('async secure-random-string', {
defer: true,
fn (defer) {
fn(defer) {
srs(() => {
defer.resolve()
})
}
})
.add('uid', {
defer: true,
fn (defer) {
fn(defer) {
uid(14).then(() => {
defer.resolve()
})
Expand Down
8 changes: 4 additions & 4 deletions test/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const LENGTH = ALPHABET.length

let nanoid2 = customAlphabet(ALPHABET, LENGTH)

function print (number) {
function print(number) {
return String(Math.floor(number * 100))
.replace(/\d{6}$/, ',$&')
.replace(/\d{3}$/, ',$&')
}

function printDistr (title, fn) {
function printDistr(title, fn) {
let data = calcDistr(title, fn)
let keys = Object.keys(data.chars)
let length = keys.length
Expand All @@ -41,7 +41,7 @@ function printDistr (title, fn) {
</section>`
}

function calcDistr (title, fn) {
function calcDistr(title, fn) {
let chars = {}

let ids = []
Expand Down Expand Up @@ -85,7 +85,7 @@ let tasks = [
})
]

function run () {
function run() {
if (tasks.length === 0) return
let task = tasks.shift()
task()
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
global.crypto = {
getRandomValues (array) {
getRandomValues(array) {
for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256)
}
Expand Down Expand Up @@ -107,7 +107,7 @@ for (let type of ['node', 'browser']) {
describe('customRandom', () => {
it('supports generator', () => {
let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]
function fakeRandom (size) {
function fakeRandom(size) {
let bytes = []
for (let i = 0; i < size; i += sequence.length) {
bytes = bytes.concat(sequence.slice(0, size - i))
Expand Down
2 changes: 1 addition & 1 deletion test/react-native-polyfill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ global.navigator = {
product: 'ReactNative'
}
global.crypto = {
getRandomValues (array) {
getRandomValues(array) {
for (let i = 0; i < array.length; i++) {
array[i] = Math.floor(Math.random() * 256)
}
Expand Down
2 changes: 1 addition & 1 deletion test/update-prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let fs = require('fs')
let writeFile = promisify(fs.writeFile)
let readFile = promisify(fs.readFile)

async function build () {
async function build() {
let js = await readFile(join(__dirname, '..', 'index.browser.js'))
let func = 'export ' + js.toString().match(/(let nanoid [\W\w]*)\s*module/)[1]
let { code } = await minify(func)
Expand Down
Loading

0 comments on commit 1277ad4

Please sign in to comment.