-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ArrayBufferPredicate
, DataViewPredicate
, TypedArrayPredicate
(
#173)
- Loading branch information
Showing
9 changed files
with
572 additions
and
29 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
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,41 @@ | ||
import {Predicate} from './predicate'; | ||
|
||
export class ArrayBufferPredicate<T extends ArrayBufferLike> extends Predicate<T> { | ||
/** | ||
Test an array buffer to have a specific byte length. | ||
@param byteLength - The byte length of the array buffer. | ||
*/ | ||
byteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength === byteLength | ||
}); | ||
} | ||
|
||
/** | ||
Test an array buffer to have a minimum byte length. | ||
@param byteLength - The minimum byte length of the array buffer. | ||
*/ | ||
minByteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength >= byteLength, | ||
negatedMessage: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength - 1}\`, got \`${value.byteLength}\`` | ||
}); | ||
} | ||
|
||
/** | ||
Test an array buffer to have a minimum byte length. | ||
@param length - The minimum byte length of the array buffer. | ||
*/ | ||
maxByteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength <= byteLength, | ||
negatedMessage: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength + 1}\`, got \`${value.byteLength}\`` | ||
}); | ||
} | ||
} |
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,48 @@ | ||
import {Predicate, PredicateOptions} from './predicate'; | ||
|
||
export class DataViewPredicate extends Predicate<DataView> { | ||
/** | ||
@hidden | ||
*/ | ||
constructor(options?: PredicateOptions) { | ||
super('DataView', options); | ||
} | ||
|
||
/** | ||
Test a DataView to have a specific byte length. | ||
@param byteLength - The byte length of the DataView. | ||
*/ | ||
byteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength === byteLength | ||
}); | ||
} | ||
|
||
/** | ||
Test a DataView to have a minimum byte length. | ||
@param byteLength - The minimum byte length of the DataView. | ||
*/ | ||
minByteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength >= byteLength, | ||
negatedMessage: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength - 1}\`, got \`${value.byteLength}\`` | ||
}); | ||
} | ||
|
||
/** | ||
Test a DataView to have a minimum byte length. | ||
@param length - The minimum byte length of the DataView. | ||
*/ | ||
maxByteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength <= byteLength, | ||
negatedMessage: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength + 1}\`, got \`${value.byteLength}\`` | ||
}); | ||
} | ||
} |
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,80 @@ | ||
import {TypedArray} from 'type-fest'; | ||
import {Predicate} from './predicate'; | ||
|
||
export class TypedArrayPredicate<T extends TypedArray> extends Predicate<T> { | ||
/** | ||
Test a typed array to have a specific byte length. | ||
@param byteLength - The byte length of the typed array. | ||
*/ | ||
byteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength === byteLength | ||
}); | ||
} | ||
|
||
/** | ||
Test a typed array to have a minimum byte length. | ||
@param byteLength - The minimum byte length of the typed array. | ||
*/ | ||
minByteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength >= byteLength, | ||
negatedMessage: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength - 1}\`, got \`${value.byteLength}\`` | ||
}); | ||
} | ||
|
||
/** | ||
Test a typed array to have a minimum byte length. | ||
@param length - The minimum byte length of the typed array. | ||
*/ | ||
maxByteLength(byteLength: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a maximum byte length of \`${byteLength}\`, got \`${value.byteLength}\``, | ||
validator: value => value.byteLength <= byteLength, | ||
negatedMessage: (value, label) => `Expected ${label} to have a minimum byte length of \`${byteLength + 1}\`, got \`${value.byteLength}\`` | ||
}); | ||
} | ||
|
||
/** | ||
Test a typed array to have a specific length. | ||
@param length - The length of the typed array. | ||
*/ | ||
length(length: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have length \`${length}\`, got \`${value.length}\``, | ||
validator: value => value.length === length | ||
}); | ||
} | ||
|
||
/** | ||
Test a typed array to have a minimum length. | ||
@param length - The minimum length of the typed array. | ||
*/ | ||
minLength(length: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a minimum length of \`${length}\`, got \`${value.length}\``, | ||
validator: value => value.length >= length, | ||
negatedMessage: (value, label) => `Expected ${label} to have a maximum length of \`${length - 1}\`, got \`${value.length}\`` | ||
}); | ||
} | ||
|
||
/** | ||
Test a typed array to have a maximum length. | ||
@param length - The maximum length of the typed array. | ||
*/ | ||
maxLength(length: number) { | ||
return this.addValidator({ | ||
message: (value, label) => `Expected ${label} to have a maximum length of \`${length}\`, got \`${value.length}\``, | ||
validator: value => value.length <= length, | ||
negatedMessage: (value, label) => `Expected ${label} to have a minimum length of \`${length + 1}\`, got \`${value.length}\`` | ||
}); | ||
} | ||
} |
Oops, something went wrong.