1.3.3 • Published 2 years ago

@d-cat/utility-api v1.3.3

Weekly downloads
9
License
ISC
Repository
gitlab
Last release
2 years ago

Getting started with @d-cat/utility-api

codecov

@d-cat/utility-api is an API that offers some frequently used methods within the @d-cat namespace.

Install

npm i @d-cat/utility-api

Usage

The API contains several helpers which helps you to develop tags and tag managers faster within the D-CAT framework.

cleanUrl({ url, trailingSlash = true, addFilterParams = [] }: { url:string, trailingSlash?: boolean, addFilterParams: string[] }): string

Clear the URL of some predefined PII.

import cleanUrl from '@d-cat/utility-api/clean-url';

const url = 'username=foo&password=bar&id=3';

cleanUrl(url);
// => id=3

cloneObject<T extends object>(obj: T): T

Deep clone the object.

import cloneObject from '@d-cat/utility-api/clone-object';

const obj = { foo: 'bar' }
const obj2 = cloneObject(obj);

obj2.baz = 'foobar'

console.log(obj)
// => { foo: 'bar' }

deletePropAtPath<T extends object>(path: string, obj: T): void

Delete a property in an object.

import dltProp from '@d-cat/utility-api/del-prop-at-path';

const obj = { foo: 'bar' }

dltProp('foo.bar', obj)

console.log(obj)
// => { }

getCookie(cookieName: string): string

Get a cookie by name.

import getCookie from '@d-cat/utility-api/get-cookie';

getCookie('myCookie')
// => myCookie

getDifferences (obj1:object, obj2: object): string[]

Get deep differences between 2 objects.

import getDiff from '@d-cat/utility-api/get-differences';

const obj = { foo: 'bar' };
const obj2 = { bar: { foobaz: 'baz'} }

getDiff(obj, obj2);
// => ['foo', 'bar', 'bar.foobaz']

getDocument(): Document

Get document.

import getDocument from '@d-cat/utility-api/get-document';

getDocument()
// => Document

getGlobal<T extends typeof globalThis | NodeJS.Global | Window>(): T

Get the global scope of the current environment.

import getGlobal from '@d-cat/utility-api/get-global';

interface IGlobal extends NodeJS.global {
    _ddm: { emit: Function }
}

const root = getGlobal<IGlobal>();
// => NodeJS.global & _ddm

getParam(sourceStr: string, param: string): string

Return value of parameter in a given string.

import getParam from '@d-cat/utility-api/get-param';

getParam('vodafone.nl?foo=bar', 'foo');
// => 'bar'

getPropAtPath<T>(path: string, obj?: object): T

Get a property at a dot notated path.

import getPropAtPath from '@d-cat/utility-api/get-prop-at-path';

const obj = { foo: { bar: 'baz' } };
getPropAtPath<string>('foo.bar', obj);
// => 'baz'

getRootDomain(): string

Get the domains root.

import getDomain from '@d-cat/utility-api/get-root-domain';

getDomain()
// => 'vodafoneziggo.com'

getSearchParams(): {[index:string]:any } | string

Returns an object with search parameters. It should be the Object.values equivalent of new URL().searchParams.

import getSearchParams from '@d-cat/utility-api/get-search-params';

getSearchParams('vodafone.nl?foo=bar&baz=foobar')
// => { foo: 'bar', baz: 'foobar' }

getWindow<T extends Window>(): T

Get window.

import getWindow from '@d-cat/utility-api/get-window';

getWindow()
// => Window

injectPixel(url: string): Promise<void>

Injects pixel in body.

import injectPixel from '@d-cat/utility-api/inject-pixel'

injectPixel.then( () => {
    // your logic
})

injectScript(url: string): Promise<void>

Injects script in head.

import injectScript from '@d-cat/utility-api/inject-script'

injectScript.then( () => {
    // your logic
})

isArray(arr: any): boolean

Checks whether a given object is in fact an Array.

import isArray from '@d-cat/utility-api/is-array'

isArray([])
// => true

isArray({})
// => false

isInteger(int: any): boolean

Checks whether a given value is an integer.

import isInteger from '@d-cat/utility-api/is-integer'

isInteger(1)
// => true

isInteger('1')
// => false

isObject(obj: any): boolean

Checks whether a given value is a real object.

import isObject from '@d-cat/utility-api/is-object'

isObject({})
// => true

isObject([])
// => false

isString(str: any): boolean

Checks whether a given value is a string.

import isString from '@d-cat/utility-api/is-string'

isString('string')
// => true

isString([])
// => false

mergeObjects<A extends object, B extends object>(dist: A, ...sources: B[]): Spread<A, B>

Merge objects into destination object.

import mergeObjects from '@d-cat/utility-api/merge-objects'

mergeObjects({}, {foo:1}, {bar: 2}, {baz: 3});
// => { bar: 2, baz: 3, foo: 1 }

setCookie({ name: string, value:any, days?: number, path?: string, domain?:string }): void

Set cookie.

import setCookie from '@d-cat/utility-api/set-cookie'

setCookie({ name: 'foo', value: 'bar' })
// => void

setPropAtPath<T>(path: string, value: T, obj: { [index: string]: any }): T

Set property in a dot notated path of an object.

import setPropAtPath from '@d-cat/utility-api/set-prop-at-path'

const obj = { foo: { bar: 'baz' } }
setPropAtPath({ path: 'foo.baz', value: 2, obj})
// => { foo: { bar: 'baz', baz: 2 } }

uuid(): string

Returns a uuid.

import uuid from '@d-cat/utility-api/create-uuid'

uuid()
// => xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx

Regex

The regex subdirectory contains a set of regex utility helpers.

hasCustomerId = (str:string): boolean

import { hasCustomerId } from '@d-cat/utility-api/regex'

hasCustomerId('klantnr: 1234')
// => true

replaceCustomerId = (str:string): string

import { replaceCustomerId } from '@d-cat/utility-api/regex'

replaceCustomerId('klantnr: 1234')
// => 'klanrnr: [MASKED_CUSTOMER_ID]'

hasPhoneNumber = (str:string): boolean

import { hasPhoneNumber } from '@d-cat/utility-api/regex'

hasPhoneNumber('0612345678')
// => true

replacePhoneNumber = (str:string): string

import { replacePhoneNumber } from '@d-cat/utility-api/regex'

replacePhoneNumber('0612345678')
// => '[MASKED_PHONENR]'

hasZipCode = (str:string): boolean

import { hasZipCode } from '@d-cat/utility-api/regex'

hasZipCode('1111 AA')
// => true

replaceZipCode = (str:string): string

import { replaceZipCode } from '@d-cat/utility-api/regex'

replaceZipCode('1111 AA')
// => '[MASKED_ZIPCODE]'

hasEmailAddress = (str:string): boolean

import { hasEmailAddress } from '@d-cat/utility-api/regex'

hasEmailAddress('d-cat@vodafoneziggo.com')
// => true

replaceEmailAddress = (str:string): string

import { replaceEmailAddress } from '@d-cat/utility-api/regex'

replaceEmailAddress('d-cat@vodafoneziggo.com')
// => '[MASKED_EMAIL]'

hasIBAN = (str:string): boolean

import { hasIBAN } from '@d-cat/utility-api/regex'

hasIBAN('NL00ABNA0000123456')
// => true

replaceIBAN = (str:string): string

import { replaceIBAN } from '@d-cat/utility-api/regex'

replaceIBAN('NL00ABNA0000123456')
// => '[MASKED_IBAN]'