Skip to content

Commit

Permalink
remove lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed May 9, 2021
1 parent d3f1b3e commit 2ad5ef0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"codemirror": "^5.59.1",
"file-saver": "^2.0.2",
"gl-matrix": "^3.3.0",
"lodash": "^4.17.19",
"rc-slider": "^9.7.2",
"react": ">= 16.8.0",
"react-dom": ">= 16.8.0",
Expand All @@ -43,7 +42,6 @@
"@types/hapi__hapi": "^20.0.3",
"@types/hapi__inert": "^5.2.2",
"@types/jest": "^25.2.3",
"@types/lodash": "^4.14.152",
"@types/node-fetch": "^2.5.10",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
Expand Down
2 changes: 1 addition & 1 deletion src/camera/Camera2d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mat2d, vec2 } from 'gl-matrix'
import { sample } from 'lodash'

import { Immutable } from '~/types/immutable'
import { sample } from '~/util/random'

export class Camera2d {
private position: vec2
Expand Down
11 changes: 5 additions & 6 deletions src/tools/map/Editor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EventEmitter } from 'events'

import { mat2d, vec2 } from 'gl-matrix'
import * as _ from 'lodash'

import { Camera2d } from '~/camera/Camera2d'
import { TILE_SIZE } from '~/constants'
Expand Down Expand Up @@ -96,8 +95,8 @@ export class Editor {
this.cursorTilePos = undefined
this.brush = {
mode: BrushMode.TERRAIN,
terrain: _.first(TERRAIN_TYPES)!,
entity: _.first(ENTITY_TYPES)!,
terrain: TERRAIN_TYPES[0],
entity: ENTITY_TYPES[0],
}

this.showTerrain = true
Expand Down Expand Up @@ -164,8 +163,8 @@ export class Editor {
} else {
this.brush.terrain =
TERRAIN_TYPES[
(TERRAIN_TYPES.indexOf(this.brush.terrain) + 1) %
TERRAIN_TYPES.length
(TERRAIN_TYPES.indexOf(this.brush.terrain) + 1) %
TERRAIN_TYPES.length
]
}

Expand All @@ -188,7 +187,7 @@ export class Editor {
} else {
this.brush.entity =
ENTITY_TYPES[
(ENTITY_TYPES.indexOf(this.brush.entity) + 1) % ENTITY_TYPES.length
(ENTITY_TYPES.indexOf(this.brush.entity) + 1) % ENTITY_TYPES.length
]
}

Expand Down
3 changes: 1 addition & 2 deletions src/util/__test__/PriorityQueue.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { shuffle } from 'lodash'

import { PriorityQueue } from '../PriorityQueue'
import { shuffle } from '../random'

const permutations = <T>(values: Array<T>): Array<Array<T>> => {
if (values.length === 0) {
Expand Down
46 changes: 46 additions & 0 deletions src/util/__test__/random.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { sample, shuffle } from '../random'

describe('shuffle', () => {
it('basic test', () => {
const a = [0]
for (let i = 0; i < 100; i++) {
expect(shuffle(a)).toEqual(a)
}

const b = [0, 1, 2, 3, 4, 5]
const outOfPosition = new Set<number>()
testb: for (let i = 0; i < 100; i++) {
const shuffled = shuffle(b)
for (let j = 0; j < shuffled.length; j++) {
if (j !== shuffled[j]) {
outOfPosition.add(j)
if (outOfPosition.size === b.length) {
break testb
}
}
}
}
expect(outOfPosition.size).toBe(b.length)
})
})

describe('sample', () => {
it('basic test', () => {
const a = [0]
for (let i = 0; i < 100; i++) {
expect(sample(a)).toBe(0)
}

const b = [0, 1, 2, 3, 4, 5]
const sampled = new Set<number>()
for (let i = 0; i < 100; i++) {
const v = sample(b)
expect(v).toBeDefined()
sampled.add(v)
if (sampled.size === b.length) {
break
}
}
expect(sampled.size).toBe(b.length)
})
})
16 changes: 16 additions & 0 deletions src/util/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function shuffle<T>(items: T[]): T[] {
const res = items.slice()
let prev = Math.floor(Math.random() * items.length)
for (let i = 0; i < items.length; i++) {
const next = Math.floor(Math.random() * items.length)
const t = res[prev]
res[prev] = res[next]
res[next] = t
prev = next
}
return res
}

export function sample<T>(items: T[]): T {
return items[Math.floor(Math.random() * items.length)]
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1609,11 +1609,6 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash@^4.14.152":
version "4.14.167"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.167.tgz#ce7d78553e3c886d4ea643c37ec7edc20f16765e"
integrity sha512-w7tQPjARrvdeBkX/Rwg95S592JwxqOjmms3zWQ0XZgSyxSLdzWaYH3vErBhdVS/lRBX7F8aBYcYJYTr5TMGOzw==

"@types/mime-db@*":
version "1.43.0"
resolved "https://registry.yarnpkg.com/@types/mime-db/-/mime-db-1.43.0.tgz#c91a4fcae0d6cfe19f2452b3b3e9fc2b95aa9a52"
Expand Down

0 comments on commit 2ad5ef0

Please sign in to comment.