Browse Source

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

orm-types
Garrett Mills 7 months ago
parent
commit
d251f8bc15
  1. 8
      src/util/support/Pipe.ts
  2. 6
      src/util/support/string.ts
  3. 5
      src/util/support/types.ts

8
src/util/support/Pipe.ts

@ -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
}

6
src/util/support/string.ts

@ -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)
})

5
src/util/support/types.ts

@ -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