-
-
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.
- Loading branch information
Showing
21 changed files
with
188 additions
and
17 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.43.0 | ||
|
||
### Minor Changes | ||
|
||
- add 3rd party helpers (no deps) | ||
|
||
## 0.42.2 | ||
|
||
### Patch 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { describe, expect, test } from "@jest/globals"; | ||
import { cyclicalItem } from "./cyclicalItem"; | ||
|
||
describe("cyclicalItem", () => { | ||
test("works", async () => { | ||
expect(cyclicalItem([], 0)).toBe(undefined); | ||
expect(cyclicalItem([], 1)).toBe(undefined); | ||
}); | ||
|
||
test("works", async () => { | ||
expect(cyclicalItem([1], 0)).toBe(1); | ||
expect(cyclicalItem([1], 1)).toBe(1); | ||
}); | ||
|
||
test("works", async () => { | ||
expect(cyclicalItem([1, 2, 3], 0)).toBe(1); | ||
expect(cyclicalItem([1, 2, 3], 2)).toBe(3); | ||
expect(cyclicalItem([1, 2, 3], 3)).toBe(1); | ||
expect(cyclicalItem([1, 2, 3], 30)).toBe(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @returns element from array at index, if index is greater than array length, it will loop back to the start of the array | ||
*/ | ||
export const cyclicalItem = <T>(array: T[], index: number): T => { | ||
return array[index % array.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,4 +1,5 @@ | ||
import { PlainObject } from "../types"; | ||
import { ObjectKey, PlainObject } from "../types"; | ||
import { getKeys } from "./getKeys"; | ||
|
||
export const firstKey = (arg: PlainObject): string => getKeys(arg)[0]; | ||
export const firstKey = <T extends PlainObject>(arg: T): ObjectKey<T> => | ||
getKeys(arg)[0]; |
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,4 @@ | ||
import { PlainObject } from "../types"; | ||
import { ObjectValue, PlainObject } from "../types"; | ||
|
||
export const firstValue = (arg: PlainObject): any => Object.values(arg)[0]; | ||
export const firstValue = <T extends PlainObject>(arg: T): ObjectValue<T> => | ||
Object.values(arg)[0]; |
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,12 +1,14 @@ | ||
export const getKeys = (arg: object) => { | ||
return Object.keys(arg).concat(getEnumerableOwnPropertySymbols(arg)); | ||
import { ObjectKeys, PlainObject } from "../types"; | ||
|
||
export const getKeys = <T extends PlainObject>(obj: T): ObjectKeys<T> => { | ||
return Object.keys(obj).concat(getEnumerableOwnPropertySymbols(obj)); | ||
}; | ||
|
||
// Object.keys does not return enumerable symbols | ||
export const getEnumerableOwnPropertySymbols = (arg: object): any[] => { | ||
export const getEnumerableOwnPropertySymbols = (obj: object): any[] => { | ||
return Object.getOwnPropertySymbols | ||
? Object.getOwnPropertySymbols(arg).filter(function (symbol) { | ||
return Object.propertyIsEnumerable.call(arg, symbol); | ||
? Object.getOwnPropertySymbols(obj).filter(function (symbol) { | ||
return Object.propertyIsEnumerable.call(obj, symbol); | ||
}) | ||
: []; | ||
}; |
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,13 @@ | ||
import { describe, expect, test } from "@jest/globals"; | ||
import { mergeArrays } from "./mergeArrays"; | ||
|
||
describe("mergeArrays", () => { | ||
test("array", async () => { | ||
expect(mergeArrays([], [])).toStrictEqual([]); | ||
expect(mergeArrays([1, 2, 3], [1])).toStrictEqual([1, 2, 3]); | ||
expect(mergeArrays([1, 2, 3, 3], [1, 3])).toStrictEqual([1, 2, 3, 3]); | ||
expect(mergeArrays([1, 2, 3, 3], [1, 3, 4, 4])).toStrictEqual([ | ||
1, 2, 3, 3, 4, 4, | ||
]); | ||
}); | ||
}); |
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,11 @@ | ||
/** | ||
* @description Merge two arrays, unique values, no options | ||
* @example mergeArrays([1,2,3], [2,3,4]) => [1,2,3,4] | ||
*/ | ||
export const mergeArrays = (arrayA: any[], arrayB: any[]) => { | ||
return arrayA.concat( | ||
arrayB.filter((item) => { | ||
return !arrayA.includes(item); // TODO: use isSame for objects | ||
}) | ||
); | ||
}; |
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,7 +1,9 @@ | ||
export * from "./checks"; | ||
export * from "./formatters"; | ||
export * from "./helpers"; | ||
export * from "./math"; | ||
export * from "./prisma"; | ||
export * from "./random"; | ||
export * from "./trpc"; | ||
export * from "./types"; | ||
export * from "./validators"; | ||
export * from "./formatters"; |
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,31 @@ | ||
import { describe, expect, test } from "@jest/globals"; | ||
import { percentageChange } from "./percentageChange"; | ||
|
||
describe("percentageChange", () => { | ||
test("simple", async () => { | ||
expect( | ||
percentageChange({ | ||
current: 10, | ||
previous: 12, | ||
}) | ||
).toBe(-16.67); | ||
expect( | ||
percentageChange({ | ||
current: 0, | ||
previous: 12, | ||
}) | ||
).toBe(0); | ||
expect( | ||
percentageChange({ | ||
current: 0, | ||
previous: 0, | ||
}) | ||
).toBe(0); | ||
expect( | ||
percentageChange({ | ||
current: 99, | ||
previous: 0, | ||
}) | ||
).toBe(0); | ||
}); | ||
}); |
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,16 @@ | ||
import { isPositiveInt } from "../validators"; | ||
|
||
export const percentageChange = ({ | ||
previous, | ||
current, | ||
}: { | ||
previous: number; | ||
current: number; | ||
}): number => { | ||
if (!isPositiveInt(previous) || !isPositiveInt(current)) return 0; | ||
if (current === 0 && previous === 0) return 0; | ||
if (current === 0 && previous !== 0) return -100; | ||
if (current !== 0 && previous === 0) return 100; | ||
const perChange = ((current - previous) * 100) / previous; | ||
return parseFloat(perChange.toFixed(2)); | ||
}; |
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,18 @@ | ||
/** | ||
* Useful for no-op queries where you want to make sure the error is a unique constraint error. | ||
* @example | ||
* try { | ||
* await query(); | ||
* } catch (e) { | ||
* checkIsPrismaUniqueConstraintError(e); | ||
* // carry on with other stuff, it is a unique constraint error | ||
* } | ||
* @link https://www.prisma.io/docs/concepts/components/prisma-client/handling-exceptions-and-errors | ||
* @link https://www.prisma.io/docs/reference/api-reference/error-reference#p2002 | ||
*/ | ||
export const checkIsPrismaRefMissingError = (error: Error) => { | ||
if (!isPrismaRefMissingError(error)) throw error; | ||
}; | ||
|
||
export const isPrismaRefMissingError = (error: Error & { code?: string }) => | ||
error.code === "P2025"; |
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,19 @@ | ||
/** | ||
* Useful for no-op queries where you want to make sure the error is a unique constraint error. | ||
* @example | ||
* try { | ||
* await query(); | ||
* } catch (e) { | ||
* checkIsPrismaUniqueConstraintError(e); | ||
* // carry on with other stuff, it is a unique constraint error | ||
* } | ||
* @link https://www.prisma.io/docs/concepts/components/prisma-client/handling-exceptions-and-errors | ||
* @link https://www.prisma.io/docs/reference/api-reference/error-reference#p2002 | ||
*/ | ||
export const checkIsPrismaUniqueConstraintError = (error: Error) => { | ||
if (!isPrismaUniqueConstraintError(error)) throw error; | ||
}; | ||
|
||
export const isPrismaUniqueConstraintError = ( | ||
error: Error & { code?: string } | ||
) => error.code === "P2002"; |
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 @@ | ||
export * from "./prismaDateRange"; |
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,16 @@ | ||
import { parseDate } from "../helpers/parseDate"; | ||
import { DateRange } from "../types/Date"; | ||
|
||
export const prismaDateRange = ({ startDate, endDate }: DateRange) => { | ||
const gte = parseDate(startDate); | ||
const lt = parseDate(endDate); | ||
|
||
if (!gte || !lt) { | ||
throw new Error("prismaDateRange: Invalid date range"); | ||
} | ||
|
||
return { | ||
gte, | ||
lt, | ||
}; | ||
}; |
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,7 @@ | ||
import { PlainObject } from "../types"; | ||
|
||
export const formatTrpcInputQueryString = (input: PlainObject) => { | ||
return new URLSearchParams({ | ||
input: JSON.stringify(input), | ||
}); | ||
}; |
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 @@ | ||
export * from "./formatTrpcInputQueryString"; |