-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Non uniform distro and other functions
- Loading branch information
Showing
19 changed files
with
165 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# deverything | ||
|
||
## 0.36.0 | ||
|
||
### Minor Changes | ||
|
||
- non-uniform distro | ||
|
||
## 0.35.0 | ||
|
||
### Minor Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let id = 1; // don't start with 0, to be closer to SQL autoincrement | ||
|
||
export const incrementalId = () => { | ||
return id++; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PlainObject } from "../types"; | ||
|
||
export const keysLength = <T extends PlainObject>(obj: T) => { | ||
return Object.keys(obj).length; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export const last = <T>(arr?: T[]): T | undefined => arr?.[arr.length - 1]; | ||
import { lastIndex } from "./lastIndex"; | ||
|
||
// Assume the array is not empty, otherwise the result needs to be banged all the time | ||
export const last = <T>(arr: T[]): T => arr[lastIndex(arr)]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const lastIndex = (array: any[]): number => { | ||
return array.length - 1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
import { lastIndex } from "../helpers/lastIndex"; | ||
import { last } from "../helpers/last"; | ||
import { randomInt } from "./randomInt"; | ||
import { sum } from "../math/sum"; | ||
|
||
export const randomArrayItem = <T>(array: T[]): T => { | ||
return array[randomInt(0, array.length - 1)]; | ||
export const randomArrayItem = <T>( | ||
array: T[], | ||
{ weights }: { weights?: number[] } = {} | ||
): T => { | ||
if (weights && weights.length === array.length) { | ||
const totalWeight = sum(weights); | ||
let random = Math.random() * totalWeight; | ||
|
||
for (let i = 0; i < weights.length; i++) { | ||
random -= weights[i]; | ||
if (random <= 0) { | ||
return array[i]; | ||
} | ||
} | ||
|
||
// In case of rounding errors, return the last item | ||
return last(array); | ||
} | ||
|
||
return array[randomInt(0, lastIndex(array))]; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
let id = 1; // don't start with 0, to be closer to SQL autoincrement | ||
|
||
/** | ||
* @deprecated use incrementalId() instead, as this one is not random and could cause confusion | ||
*/ | ||
export const randomNumericId = () => { | ||
return id++; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, it, expect } from "@jest/globals"; | ||
import { randomPath } from "./randomPath"; | ||
|
||
describe(`randomPath`, () => { | ||
it(`works`, () => { | ||
expect(randomPath().includes("/")).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { array } from "../helpers"; | ||
import { randomWord } from "./randomWord"; | ||
|
||
export const randomPath = ({ | ||
depth = 3, | ||
}: { | ||
depth?: number; | ||
} = {}) => { | ||
return array(depth, randomWord).join("/"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import { WORDS } from "../constants/words"; | ||
import { NOUNS, VERBS, WORDS } from "../constants/words"; | ||
import { randomArrayItem } from "./randomArrayItem"; | ||
|
||
export const randomWord = () => { | ||
return randomArrayItem(WORDS); | ||
}; | ||
|
||
export const randomNoun = () => { | ||
return randomArrayItem(NOUNS); | ||
}; | ||
|
||
export const randomVerb = () => { | ||
return randomArrayItem(VERBS); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { lastIndex } from "../helpers/lastIndex"; | ||
|
||
export const isLastIndex = (index: number, array: any[]): boolean => { | ||
return index === array.length - 1; | ||
return index === lastIndex(array); | ||
}; |