Skip to content

Commit

Permalink
using is utils
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 18, 2024
1 parent c4196a4 commit c87e580
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 35 deletions.
4 changes: 2 additions & 2 deletions packages/destinations/node/meta/src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ServerEvent,
UserData,
} from 'facebook-nodejs-business-sdk';
import { getMappingValue } from '@elbwalker/utils';
import { getMappingValue, isString } from '@elbwalker/utils';

export const push: PushFn = async function (event, config, mapping) {
const {
Expand Down Expand Up @@ -53,7 +53,7 @@ export const mapEvent = (
if (user) {
// IDs
const ids = [user.id, user.device, user.session, user.hash]
.filter((id) => typeof id === 'string')
.filter(isString)
.map(lower);

if (ids.length) userData = userData.setExternalIds(ids);
Expand Down
4 changes: 2 additions & 2 deletions packages/destinations/web/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Custom, Destination } from './types';
import { isDefined, sendWeb } from '@elbwalker/utils';
import { isArray, isDefined, sendWeb } from '@elbwalker/utils';

// Types
export * as DestinationWebAPI from './types';
Expand All @@ -16,7 +16,7 @@ export const destinationWebAPI: Destination = {
if (!url) return;

const data = isDefined(options.data) ? options.data : event;
const value = Array.isArray(data) ? data[0] : data;
const value = isArray(data) ? data[0] : data;
const body = transform
? transform(value, config, mapping) // Transform event data
: JSON.stringify(value);
Expand Down
9 changes: 5 additions & 4 deletions packages/sources/datalayer/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable prefer-rest-params */
import type { DataLayer } from '../types';
import { sourceDataLayer } from '..';
import { isArray } from '@elbwalker/utils';

describe('source dataLayer', () => {
const elb = jest.fn(); //.mockImplementation(console.log);
let dataLayer: DataLayer;

beforeEach(() => {
window.dataLayer = [];
dataLayer = window.dataLayer;
dataLayer = window.dataLayer as DataLayer;
});

const gtag: Gtag.Gtag = function () {
Expand All @@ -24,8 +25,8 @@ describe('source dataLayer', () => {
expect(window.dataLayer).toBeUndefined();

sourceDataLayer({ elb });
expect(Array.isArray(window.dataLayer)).toBe(true);
expect(window.dataLayer!.length).toBe(0);
expect(isArray(window.dataLayer)).toBe(true);
expect((window.dataLayer as DataLayer)!.length).toBe(0);
});

test('init existing', () => {
Expand All @@ -49,7 +50,7 @@ describe('source dataLayer', () => {
expect(window.foo).toBeUndefined();

sourceDataLayer({ elb, name: 'foo' });
expect(Array.isArray(window.foo)).toBe(true);
expect(isArray(window.foo)).toBe(true);
});

test('original arguments', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/sources/walkerjs/src/lib/handle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Hooks, WalkerOS } from '@elbwalker/types';
import type { On, SourceWalkerjs, DestinationWeb } from '../types';
import { Const, assign, isObject, isSameType } from '@elbwalker/utils';
import { Const, assign, isArray, isObject, isSameType } from '@elbwalker/utils';
import { isElementOrDocument } from './helper';
import { initScopeTrigger, ready } from './trigger';
import { getState } from './state';
Expand Down Expand Up @@ -47,7 +47,7 @@ export function handleCommand(
addHook(instance, data as keyof Hooks.Functions, options);
break;
case Const.Commands.Init: {
const elems: unknown[] = Array.isArray(data) ? data : [data || document];
const elems: unknown[] = isArray(data) ? data : [data || document];
elems.forEach((elem) => {
isElementOrDocument(elem) && initScopeTrigger(instance, elem);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/sources/walkerjs/src/lib/on.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WalkerOS } from '@elbwalker/types';
import type { On, SourceWalkerjs } from '../';
import { Const, tryCatch } from '@elbwalker/utils';
import { Const, isArray, tryCatch } from '@elbwalker/utils';

export function on(
instance: SourceWalkerjs.Instance,
Expand All @@ -9,7 +9,7 @@ export function on(
) {
const on = instance.on;
const onType: Array<On.Options> = on[type] || [];
const options = Array.isArray(option) ? option : [option];
const options = isArray(option) ? option : [option];

options.forEach((option) => {
onType.push(option);
Expand Down
11 changes: 9 additions & 2 deletions packages/sources/walkerjs/src/lib/walker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Walker } from '../types';
import type { WalkerOS } from '@elbwalker/types';
import { Const, assign, castValue, getAttribute, trim } from '@elbwalker/utils';
import {
Const,
assign,
castValue,
getAttribute,
isArray,
trim,
} from '@elbwalker/utils';

export function getElbAttributeName(
prefix: string,
Expand Down Expand Up @@ -55,7 +62,7 @@ export function getElbValues(

if (key.endsWith('[]')) {
key = key.slice(0, -2); // Remove [] symbol
if (!Array.isArray(values[key])) values[key] = [];
if (!isArray(values[key])) values[key] = [];
(values[key] as WalkerOS.PropertyType[]).push(castValue(val));
} else {
values[key] = castValue(val);
Expand Down
5 changes: 2 additions & 3 deletions packages/utils/src/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getEvent,
getMappingEvent,
getMappingValue,
isString,
} from '../core';

describe('getMappingEvent', () => {
Expand Down Expand Up @@ -263,9 +264,7 @@ describe('getMappingValue', () => {

test('validate', () => {
const event = createEvent();
const mockValidate = jest.fn((value) => {
return typeof value === 'string';
});
const mockValidate = jest.fn(isString);

// validation passed
expect(
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/core/byPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WalkerOS } from '@elbwalker/types';
import { isDefined } from './is';
import { isArray, isDefined } from './is';

export function getByPath(
event: unknown,
Expand All @@ -14,7 +14,7 @@ export function getByPath(
for (let index = 0; index < keys.length; index++) {
const k = keys[index];

if (k === '*' && Array.isArray(values)) {
if (k === '*' && isArray(values)) {
const remainingKeys = keys.slice(index + 1).join('.');
const result: unknown[] = [];

Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/core/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function isArray<T>(value: unknown): value is T[] {
return Array.isArray(value);
}

export function isBoolean(value: unknown): value is boolean {
return typeof value === 'boolean';
}

export function isDefined<T>(val: T | undefined): val is T {
return typeof val !== 'undefined';
}
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/core/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function processMappingValue(
const { instance } = options;

// Ensure mapping is an array for uniform processing
const mappings = Array.isArray(mapping) ? mapping : [mapping];
const mappings = isArray(mapping) ? mapping : [mapping];

// Loop over each mapping and return the first valid result
return mappings.reduce((acc, mappingItem) => {
Expand Down Expand Up @@ -103,7 +103,7 @@ function processMappingValue(
const data =
scope === 'this' ? [obj] : getMappingValue(obj, scope, options);

if (Array.isArray(data)) {
if (isArray(data)) {
mappingValue = data
.map((item) =>
getMappingValue(isObject(item) ? item : {}, itemMapping, options),
Expand Down
23 changes: 13 additions & 10 deletions packages/utils/src/core/property.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import type { WalkerOS } from '@elbwalker/types';
import { isArguments, isArray, isNumber, isObject } from './is';
import {
isArguments,
isArray,
isBoolean,
isDefined,
isNumber,
isObject,
isString,
} from './is';

export function isPropertyType(value: unknown): value is WalkerOS.PropertyType {
return (
typeof value === 'boolean' ||
typeof value === 'string' ||
isBoolean(value) ||
isString(value) ||
isNumber(value) ||
typeof value === 'undefined' ||
!isDefined(value) ||
(isArray(value) && value.every(isPropertyType)) ||
(isObject(value) && Object.values(value).every(isPropertyType))
);
}

export function filterValues(value: unknown): WalkerOS.Property | undefined {
if (
typeof value === 'boolean' ||
typeof value === 'string' ||
isNumber(value)
)
return value;
if (isBoolean(value) || isString(value) || isNumber(value)) return value;

if (isArguments(value)) return filterValues(Array.from(value));

Expand Down
9 changes: 5 additions & 4 deletions packages/utils/src/core/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { WalkerOS } from '@elbwalker/types';
import { tryCatch } from './tryCatch';
import { castValue } from './castValue';
import { isArray, isObject } from './is';

export function requestToData(
parameter: unknown,
Expand All @@ -19,7 +20,7 @@ export function requestToData(
keys.forEach((k, i) => {
const isLast = i === keys.length - 1;

if (Array.isArray(current)) {
if (isArray(current)) {
const index = parseInt(k, 10);
if (isLast) {
(current as Array<unknown>)[index] = castValue(value);
Expand All @@ -29,7 +30,7 @@ export function requestToData(
(isNaN(parseInt(keys[i + 1], 10)) ? {} : []);
current = (current as Array<unknown>)[index];
}
} else if (typeof current === 'object' && current !== null) {
} else if (isObject(current)) {
if (isLast) {
(current as WalkerOS.AnyObject)[k] = castValue(value);
} else {
Expand Down Expand Up @@ -57,9 +58,9 @@ export function requestToParameter(
function addParam(key: string, value: unknown) {
if (value === undefined || value === null) return;

if (Array.isArray(value)) {
if (isArray(value)) {
value.forEach((item, index) => addParam(`${key}[${index}]`, item));
} else if (typeof value === 'object' && value !== null) {
} else if (isObject(value)) {
Object.entries(value).forEach(([subKey, subValue]) =>
addParam(`${key}[${subKey}]`, subValue),
);
Expand Down

0 comments on commit c87e580

Please sign in to comment.