Skip to content

Commit

Permalink
fixes automatic eslinting for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
philihp committed Aug 30, 2022
1 parent 507e09d commit 78a2356
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
63 changes: 54 additions & 9 deletions package.json
Expand Up @@ -26,7 +26,7 @@
},
"scripts": {
"build": "tsc -p tsconfig.build.json",
"lint": "eslint src",
"lint": "eslint --ext .js,.ts src",
"prepare": "npm run build",
"test": "jest",
"test:coverage": "jest --coverage",
Expand All @@ -38,17 +38,21 @@
"fn-mt": "1.2.1"
},
"devDependencies": {
"@philihp/eslint-config": "6.0.0",
"@philihp/eslint-config": "6.0.2",
"@philihp/prettier-config": "1.0.0",
"@types/jest": "28.1.8",
"@types/jest": "29.0.0",
"@types/ramda": "0.28.15",
"@typescript-eslint/eslint-plugin": "5.36.1",
"@typescript-eslint/parser": "5.36.1",
"eslint": "8.23.0",
"eslint-import-resolver-typescript": "3.5.0",
"eslint-plugin-import": "2.26.0",
"husky": "8.0.1",
"jest": "28.1.3",
"lint-staged": "13.0.3",
"prettier": "2.7.1",
"ramda": "0.28.0",
"ts-jest": "28.0.8",
"typescript": "4.8.2"
"ts-jest": "28.0.8"
},
"jest": {
"preset": "ts-jest",
Expand All @@ -58,8 +62,8 @@
]
},
"lint-staged": {
"src/**/*.{js,jsx,json}": [
"eslint --fix"
"src/**/*.{js,jsx,json,.ts,.tsx}": [
"eslint --ext .js,.ts --fix"
]
},
"prettier": "@philihp/prettier-config",
Expand All @@ -69,8 +73,49 @@
}
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
"plugins": [
"@typescript-eslint"
],
"extends": [
"@philihp"
]
"@philihp",
"plugin:jest/all",
"plugin:@typescript-eslint/recommended"
],
"settings": {
"import/extensions": [
".js",
".ts"
],
"import/parsers": {
"@typescript-eslint/parser": [
".ts"
]
},
"import/resolver": {
"typescript": {},
"node": {
"extensions": [
".js",
".ts"
]
}
}
},
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": [
"**/*.test.ts"
]
}
]
}
}
}
26 changes: 5 additions & 21 deletions src/__tests__/index.test.ts
Expand Up @@ -68,16 +68,8 @@ describe('default', () => {
it('accepts a custom random function', () => {
expect.assertions(1)
const noise = [
0.8901547130662948,
0.3163755603600294,
0.1307072939816823,
0.1839188123121576,
0.0397594964593742,
0.2045602793853012,
0.8264361317269504,
0.5677250262815505,
0.5320779164321721,
0.5955447026062757,
0.8901547130662948, 0.3163755603600294, 0.1307072939816823, 0.1839188123121576, 0.0397594964593742,
0.2045602793853012, 0.8264361317269504, 0.5677250262815505, 0.5320779164321721, 0.5955447026062757,
]
// @ts-ignore
const pseudoShuffle = fastShuffle(() => noise.pop())
Expand All @@ -90,16 +82,8 @@ describe('default', () => {
it('accepts a custom random function that gives floats (0..1)', () => {
expect.assertions(2)
const noise = [
0.8901547130662948,
0.3163755603600294,
0.1307072939816823,
0.1839188123121576,
0.0397594964593742,
0.2045602793853012,
0.8264361317269504,
0.5677250262815505,
0.5320779164321721,
0.5955447026062757,
0.8901547130662948, 0.3163755603600294, 0.1307072939816823, 0.1839188123121576, 0.0397594964593742,
0.2045602793853012, 0.8264361317269504, 0.5677250262815505, 0.5320779164321721, 0.5955447026062757,
]
// @ts-ignore
const pseudoShuffle = fastShuffle(() => noise.pop())
Expand Down Expand Up @@ -135,7 +119,7 @@ describe('fastShuffle for reducers', () => {
const d1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
// @ts-ignore
const [d2, seedState] = fastShuffle([d1, 12345])
expect(seedState).not.toBeUndefined()
expect(seedState).toBeDefined()
expect(d2).toStrictEqual(['B', 'H', 'F', 'C', 'G', 'A', 'D', 'E'])
})

Expand Down
27 changes: 12 additions & 15 deletions src/index.ts
@@ -1,6 +1,6 @@
import { newRandGen, randNext, randRange } from 'fn-mt'

type FastShuffleState<T> = [deck: T[], seed: number];
type FastShuffleState<T> = [deck: T[], seed: number]

/**
* This is the algorithm. Random should be a function that when given
Expand All @@ -25,8 +25,7 @@ const fisherYatesShuffle = (random: (arg: number) => number) => (sourceArray: an

const randomInt = () => (Math.random() * 2 ** 32) | 0

const randomExternal = (random: () => number) => (maxIndex: number) =>
((random() / 2 ** 32) * maxIndex) | 0
const randomExternal = (random: () => number) => (maxIndex: number) => ((random() / 2 ** 32) * maxIndex) | 0

const randomInternal = (random: number) => {
let randState = newRandGen(random)
Expand All @@ -37,10 +36,8 @@ const randomInternal = (random: number) => {
}
}

const randomSwitch = (random: (number | (() => number))) =>
(typeof random === 'function') ?
randomExternal(random) :
randomInternal(random)
const randomSwitch = (random: number | (() => number)) =>
typeof random === 'function' ? randomExternal(random) : randomInternal(random)

const functionalShuffle = <T>(deck: T[], state: number): FastShuffleState<T> => {
let randState = newRandGen(state)
Expand All @@ -49,23 +46,23 @@ const functionalShuffle = <T>(deck: T[], state: number): FastShuffleState<T> =>
randState = nextState
return nextInt
}
return [fisherYatesShuffle(random)(deck), randNext(randState)[0]];
return [fisherYatesShuffle(random)(deck), randNext(randState)[0]]
}

function fastShuffle(randomSeed: number | (() => number)): <T>(deck: T[]) => T[]
function fastShuffle<T>(randomSeed: number | (() => number), deck: T[]): T[]
function fastShuffle<T>(fnParams: [deck: T[], randomSeed?: number]): FastShuffleState<T>
function fastShuffle(randomSeed: number | (() => number) | [deck: unknown[], randomSeed?: number], deck?: unknown[]) {
if (typeof randomSeed === 'object') {
const fnDeck = randomSeed[0];
const fnState = randomSeed[1] ?? randomInt();
return functionalShuffle(fnDeck, fnState);
const fnDeck = randomSeed[0]
const fnState = randomSeed[1] ?? randomInt()
return functionalShuffle(fnDeck, fnState)
}
const random = randomSwitch(randomSeed);
const shuffler = fisherYatesShuffle(random);
const random = randomSwitch(randomSeed)
const shuffler = fisherYatesShuffle(random)
// if no second param given, return a curried shuffler
if (deck === undefined) return shuffler;
return shuffler(deck);
if (deck === undefined) return shuffler
return shuffler(deck)
}

export const shuffle = <T>(deck: T[]) => fastShuffle(randomInt(), deck)
Expand Down

0 comments on commit 78a2356

Please sign in to comment.