generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Tyler J Russell <xtylerjrx@gmail.com>
- Loading branch information
1 parent
f7e3d67
commit ca5ea5f
Showing
10 changed files
with
917 additions
and
7 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
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,176 @@ | ||
import { ExpectedConstraintError } from '../lib/errors/ExpectedConstraintError'; | ||
import { Result } from '../lib/Result'; | ||
import type { IConstraint } from './base/IConstraint'; | ||
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators'; | ||
import type { TypedArray } from './util/typedArray'; | ||
|
||
export type TypedArrayConstraintName = `s.typedArray(T).${'byteLength' | 'length'}${ | ||
| 'Lt' | ||
| 'Le' | ||
| 'Gt' | ||
| 'Ge' | ||
| 'Eq' | ||
| 'Ne' | ||
| 'Range' | ||
| 'RangeInclusive' | ||
| 'RangeExclusive'}`; | ||
|
||
function typedArrayByteLengthComparator<T extends TypedArray>( | ||
comparator: Comparator, | ||
name: TypedArrayConstraintName, | ||
expected: string, | ||
length: number | ||
): IConstraint<T> { | ||
return { | ||
run(input: T) { | ||
return comparator(input.byteLength, length) // | ||
? Result.ok(input) | ||
: Result.err(new ExpectedConstraintError(name, 'Invalid Typed Array byte length', input, expected)); | ||
} | ||
}; | ||
} | ||
|
||
export function typedArrayByteLengthLt<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.byteLength < ${value}`; | ||
return typedArrayByteLengthComparator(lt, 's.typedArray(T).byteLengthLt', expected, value); | ||
} | ||
|
||
export function typedArrayByteLengthLe<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.byteLength <= ${value}`; | ||
return typedArrayByteLengthComparator(le, 's.typedArray(T).byteLengthLe', expected, value); | ||
} | ||
|
||
export function typedArrayByteLengthGt<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.byteLength > ${value}`; | ||
return typedArrayByteLengthComparator(gt, 's.typedArray(T).byteLengthGt', expected, value); | ||
} | ||
|
||
export function typedArrayByteLengthGe<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.byteLength >= ${value}`; | ||
return typedArrayByteLengthComparator(ge, 's.typedArray(T).byteLengthGe', expected, value); | ||
} | ||
|
||
export function typedArrayByteLengthEq<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.byteLength === ${value}`; | ||
return typedArrayByteLengthComparator(eq, 's.typedArray(T).byteLengthEq', expected, value); | ||
} | ||
|
||
export function typedArrayByteLengthNe<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.byteLength !== ${value}`; | ||
return typedArrayByteLengthComparator(ne, 's.typedArray(T).byteLengthNe', expected, value); | ||
} | ||
|
||
export function typedArrayByteLengthRange<T extends TypedArray>(start: number, endBefore: number): IConstraint<T> { | ||
const expected = `expected.byteLength >= ${start} && expected.byteLength < ${endBefore}`; | ||
return { | ||
run(input: T) { | ||
return input.byteLength >= start && input.byteLength < endBefore // | ||
? Result.ok(input) | ||
: Result.err(new ExpectedConstraintError('s.typedArray(T).byteLengthRange', 'Invalid Typed Array byte length', input, expected)); | ||
} | ||
}; | ||
} | ||
|
||
export function typedArrayByteLengthRangeInclusive<T extends TypedArray>(start: number, end: number) { | ||
const expected = `expected.byteLength >= ${start} && expected.byteLength <= ${end}`; | ||
return { | ||
run(input: T) { | ||
return input.byteLength >= start && input.byteLength <= end // | ||
? Result.ok(input) | ||
: Result.err( | ||
new ExpectedConstraintError('s.typedArray(T).byteLengthRangeInclusive', 'Invalid Typed Array byte length', input, expected) | ||
); | ||
} | ||
}; | ||
} | ||
|
||
export function typedArrayByteLengthRangeExclusive<T extends TypedArray>(startAfter: number, endBefore: number): IConstraint<T> { | ||
const expected = `expected.byteLength > ${startAfter} && expected.byteLength < ${endBefore}`; | ||
return { | ||
run(input: T) { | ||
return input.byteLength > startAfter && input.byteLength < endBefore // | ||
? Result.ok(input) | ||
: Result.err( | ||
new ExpectedConstraintError('s.typedArray(T).byteLengthRangeExclusive', 'Invalid Typed Array byte length', input, expected) | ||
); | ||
} | ||
}; | ||
} | ||
|
||
function typedArrayLengthComparator<T extends TypedArray>( | ||
comparator: Comparator, | ||
name: TypedArrayConstraintName, | ||
expected: string, | ||
length: number | ||
): IConstraint<T> { | ||
return { | ||
run(input: T) { | ||
return comparator(input.length, length) // | ||
? Result.ok(input) | ||
: Result.err(new ExpectedConstraintError(name, 'Invalid Typed Array length', input, expected)); | ||
} | ||
}; | ||
} | ||
|
||
export function typedArrayLengthLt<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.length < ${value}`; | ||
return typedArrayLengthComparator(lt, 's.typedArray(T).lengthLt', expected, value); | ||
} | ||
|
||
export function typedArrayLengthLe<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.length <= ${value}`; | ||
return typedArrayLengthComparator(le, 's.typedArray(T).lengthLe', expected, value); | ||
} | ||
|
||
export function typedArrayLengthGt<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.length > ${value}`; | ||
return typedArrayLengthComparator(gt, 's.typedArray(T).lengthGt', expected, value); | ||
} | ||
|
||
export function typedArrayLengthGe<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.length >= ${value}`; | ||
return typedArrayLengthComparator(ge, 's.typedArray(T).lengthGe', expected, value); | ||
} | ||
|
||
export function typedArrayLengthEq<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.length === ${value}`; | ||
return typedArrayLengthComparator(eq, 's.typedArray(T).lengthEq', expected, value); | ||
} | ||
|
||
export function typedArrayLengthNe<T extends TypedArray>(value: number): IConstraint<T> { | ||
const expected = `expected.length !== ${value}`; | ||
return typedArrayLengthComparator(ne, 's.typedArray(T).lengthNe', expected, value); | ||
} | ||
|
||
export function typedArrayLengthRange<T extends TypedArray>(start: number, endBefore: number): IConstraint<T> { | ||
const expected = `expected.length >= ${start} && expected.length < ${endBefore}`; | ||
return { | ||
run(input: T) { | ||
return input.length >= start && input.length < endBefore // | ||
? Result.ok(input) | ||
: Result.err(new ExpectedConstraintError('s.typedArray(T).lengthRange', 'Invalid Typed Array length', input, expected)); | ||
} | ||
}; | ||
} | ||
|
||
export function typedArrayLengthRangeInclusive<T extends TypedArray>(start: number, end: number): IConstraint<T> { | ||
const expected = `expected.length >= ${start} && expected.length <= ${end}`; | ||
return { | ||
run(input: T) { | ||
return input.length >= start && input.length <= end // | ||
? Result.ok(input) | ||
: Result.err(new ExpectedConstraintError('s.typedArray(T).lengthRangeInclusive', 'Invalid Typed Array length', input, expected)); | ||
} | ||
}; | ||
} | ||
|
||
export function typedArrayLengthRangeExclusive<T extends TypedArray>(startAfter: number, endBefore: number): IConstraint<T> { | ||
const expected = `expected.length > ${startAfter} && expected.length < ${endBefore}`; | ||
return { | ||
run(input: T) { | ||
return input.length > startAfter && input.length < endBefore // | ||
? Result.ok(input) | ||
: Result.err(new ExpectedConstraintError('s.typedArray(T).lengthRangeExclusive', 'Invalid Typed Array length', input, expected)); | ||
} | ||
}; | ||
} |
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 @@ | ||
const vowels = ['a', 'e', 'i', 'o', 'u']; | ||
|
||
export const aOrAn = (word: string) => { | ||
return `${vowels.includes(word[0].toLowerCase()) ? 'an' : 'a'} ${word}`; | ||
}; |
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,29 @@ | ||
export type TypedArray = | ||
| Int8Array | ||
| Uint8Array | ||
| Uint8ClampedArray | ||
| Int16Array | ||
| Uint16Array | ||
| Int32Array | ||
| Uint32Array | ||
| Float32Array | ||
| Float64Array | ||
| BigInt64Array | ||
| BigUint64Array; | ||
|
||
export const TypedArrays = { | ||
Int8Array: (x: unknown): x is Int8Array => x instanceof Int8Array, | ||
Uint8Array: (x: unknown): x is Uint8Array => x instanceof Uint8Array, | ||
Uint8ClampedArray: (x: unknown): x is Uint8ClampedArray => x instanceof Uint8ClampedArray, | ||
Int16Array: (x: unknown): x is Int16Array => x instanceof Int16Array, | ||
Uint16Array: (x: unknown): x is Uint16Array => x instanceof Uint16Array, | ||
Int32Array: (x: unknown): x is Int32Array => x instanceof Int32Array, | ||
Uint32Array: (x: unknown): x is Uint32Array => x instanceof Uint32Array, | ||
Float32Array: (x: unknown): x is Float32Array => x instanceof Float32Array, | ||
Float64Array: (x: unknown): x is Float64Array => x instanceof Float64Array, | ||
BigInt64Array: (x: unknown): x is BigInt64Array => x instanceof BigInt64Array, | ||
BigUint64Array: (x: unknown): x is BigUint64Array => x instanceof BigUint64Array, | ||
TypedArray: (x: unknown): x is TypedArray => ArrayBuffer.isView(x) && !(x instanceof DataView) | ||
} as const; | ||
|
||
export type TypedArrayName = keyof typeof TypedArrays; |
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
Oops, something went wrong.