Util: fix Pipe conditionals and add type-safe hasOwnProperty helper

orm-types
Garrett Mills 2 years ago
parent bf4a675faa
commit d251f8bc15

@ -96,7 +96,9 @@ export class Pipe<T> {
* @param op
*/
when(check: PipeCondition<T>, op: ReflexivePipeOperator<T>): Pipe<T> {
if ( (typeof check === 'function' && check(this.subject)) || check ) {
if (
(typeof check === 'function' && check(this.subject))
|| (typeof check !== 'function' && check) ) {
return Pipe.wrap(op(this.subject))
}
@ -111,7 +113,9 @@ export class Pipe<T> {
* @param op
*/
unless(check: PipeCondition<T>, op: ReflexivePipeOperator<T>): Pipe<T> {
if ( (typeof check === 'function' && check(this.subject)) || check ) {
if (
(typeof check === 'function' && check(this.subject))
|| (typeof check !== 'function' && check) ) {
return this
}

@ -36,13 +36,15 @@ export function padLeft(string: string, length: number, padWith = ' '): string {
* @param padWith
*/
export function padCenter(string: string, length: number, padWith = ' '): string {
const bit = false
let bit = false
while ( string.length < length ) {
if ( bit ) {
string = `${padWith}${string}`
} else {
string += padWith
}
bit = !bit
}
return string
@ -53,7 +55,7 @@ export function padCenter(string: string, length: number, padWith = ' '): string
* @param input
*/
export function stringToPascal(input: string): string {
return input.split(/[\s_]+/i)
return input.split(/[\s_-]+/i)
.map(part => {
return part[0].toUpperCase() + part.substr(1)
})

@ -18,3 +18,8 @@ export function isKeyof<T>(key: unknown, obj: T): key is keyof T {
return key in obj
}
/** A typescript-compatible version of Object.hasOwnProperty. */
export function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> { // eslint-disable-line @typescript-eslint/ban-types
return Object.hasOwnProperty.call(obj, prop)
}

Loading…
Cancel
Save