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 * @param op
*/ */
when(check: PipeCondition<T>, op: ReflexivePipeOperator<T>): Pipe<T> { 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)) return Pipe.wrap(op(this.subject))
} }
@ -111,7 +113,9 @@ export class Pipe<T> {
* @param op * @param op
*/ */
unless(check: PipeCondition<T>, op: ReflexivePipeOperator<T>): Pipe<T> { 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 return this
} }

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