@walkeros/core 0.0.0-next-20251219153324

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,358 @@
1
+ <p align="left">
2
+ <a href="https://www.walkeros.io">
3
+ <img title="elbwalker" src="https://www.walkeros.io/img/elbwalker_logo.png" width="256px"/>
4
+ </a>
5
+ </p>
6
+
7
+ # Core Types & Utilities for walkerOS
8
+
9
+ [Source Code](https://github.com/elbwalker/walkerOS/tree/main/packages/core)
10
+ &bull; [NPM Package](https://www.npmjs.com/package/@walkeros/core)
11
+
12
+ Core utilities are a collection of platform-agnostic functions that can be used
13
+ across all walkerOS environments. They provide standardized building blocks for
14
+ data manipulation, validation, mapping, and more.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @walkeros/core
20
+ ```
21
+
22
+ ## Quick Start
23
+
24
+ The core package provides types and utilities used across walkerOS. In a Flow
25
+ configuration:
26
+
27
+ ```json
28
+ {
29
+ "version": 1,
30
+ "flows": {
31
+ "default": {
32
+ "web": {},
33
+ "destinations": {
34
+ "api": {
35
+ "package": "@walkeros/web-destination-api",
36
+ "config": {
37
+ "url": "https://collect.example.com/events"
38
+ }
39
+ }
40
+ }
41
+ }
42
+ }
43
+ }
44
+ ```
45
+
46
+ Import utilities directly:
47
+
48
+ ```ts
49
+ import { assign, anonymizeIP, getMappingValue } from '@walkeros/core';
50
+ ```
51
+
52
+ ## Core Utilities
53
+
54
+ ### Data Manipulation
55
+
56
+ #### assign
57
+
58
+ `assign<T, U>(target: T, source: U, options?): T & U` merges two objects with
59
+ advanced merging capabilities. It has special behavior for arrays: when merging,
60
+ it concatenates arrays from both objects, removing duplicates.
61
+
62
+ ```ts
63
+ interface AssignOptions {
64
+ merge?: boolean; // Merge array properties (default: true)
65
+ shallow?: boolean; // Create shallow copy (default: true)
66
+ extend?: boolean; // Extend with new properties (default: true)
67
+ }
68
+
69
+ const obj1 = { a: 1, b: [1, 2] };
70
+ const obj2 = { b: [2, 3], c: 3 };
71
+
72
+ assign(obj1, obj2); // Returns { a: 1, b: [1, 2, 3], c: 3 }
73
+ assign(obj1, obj2, { merge: false }); // Returns { a: 1, b: [2, 3], c: 3 }
74
+ ```
75
+
76
+ #### Path Operations
77
+
78
+ ##### getByPath
79
+
80
+ `getByPath(object: unknown, path: string, defaultValue?: unknown): unknown`
81
+ accesses nested properties using dot notation. Supports wildcard `*` for array
82
+ iteration.
83
+
84
+ ```js
85
+ getByPath({ data: { id: 'wow' } }, 'data.id'); // Returns "wow"
86
+ getByPath({ nested: [1, 2, { id: 'cool' }] }, 'nested.*.id'); // Returns ['', '', 'cool']
87
+ getByPath({ arr: ['foo', 'bar'] }, 'arr.1'); // Returns "bar"
88
+ ```
89
+
90
+ ##### setByPath
91
+
92
+ `setByPath(object: WalkerOS.Event, path: string, value: unknown): WalkerOS.Event`
93
+ sets nested values using dot notation, returning a new object with the updated
94
+ value.
95
+
96
+ ```js
97
+ const updatedEvent = setByPath(event, 'data.id', 'new-value');
98
+ // Returns a new event with data.id set to 'new-value'
99
+ ```
100
+
101
+ #### clone
102
+
103
+ `clone<T>(original: T): T` creates a deep copy of objects/arrays with circular
104
+ reference handling.
105
+
106
+ ```js
107
+ const original = { foo: true, arr: ['a', 'b'] };
108
+ const cloned = clone(original);
109
+ original.foo = false; // cloned.foo remains true
110
+ ```
111
+
112
+ #### castValue
113
+
114
+ `castValue(value: unknown): WalkerOS.PropertyType` converts string values to
115
+ appropriate types (number, boolean).
116
+
117
+ ```js
118
+ castValue('123'); // Returns 123 (number)
119
+ castValue('true'); // Returns true (boolean)
120
+ castValue('hello'); // Returns 'hello' (unchanged)
121
+ ```
122
+
123
+ ### Privacy & Security
124
+
125
+ #### anonymizeIP
126
+
127
+ `anonymizeIP(ip: string): string` anonymizes IPv4 addresses by setting the last
128
+ oclet to zero.
129
+
130
+ ```js
131
+ anonymizeIP('192.168.1.100'); // Returns '192.168.1.0'
132
+ ```
133
+
134
+ #### Hashing
135
+
136
+ `getId(length?: number): string` generates random alphanumeric strings for
137
+ unique identifiers.
138
+
139
+ ```js
140
+ getId(); // Returns random 6-char string like 'a1b2c3'
141
+ getId(10); // Returns 10-character string
142
+ ```
143
+
144
+ ### Event Processing
145
+
146
+ #### getMappingValue
147
+
148
+ `getMappingValue(event: WalkerOS.Event, mapping: Mapping.Data, options?: Mapping.Options): Promise<WalkerOS.Property | undefined>`
149
+ extracts values from events using
150
+ [mapping configurations](https://www.walkeros.io/docs/destinations/event-mapping).
151
+
152
+ ```ts
153
+ // Simple path mapping
154
+ await getMappingValue(event, 'data.productId');
155
+
156
+ // Complex mapping with conditions and loops
157
+ const mapping = {
158
+ map: {
159
+ orderId: 'data.id',
160
+ products: {
161
+ loop: [
162
+ 'nested',
163
+ {
164
+ condition: (entity) => entity.entity === 'product',
165
+ map: { id: 'data.id', name: 'data.name' },
166
+ },
167
+ ],
168
+ },
169
+ },
170
+ };
171
+ await getMappingValue(event, mapping);
172
+ ```
173
+
174
+ #### getMappingEvent
175
+
176
+ `getMappingEvent(event: WalkerOS.PartialEvent, mapping?: Mapping.Rules): Promise<Mapping.Result>`
177
+ finds the appropriate mapping rule for an event.
178
+
179
+ ### Marketing & Analytics
180
+
181
+ #### getMarketingParameters
182
+
183
+ `getMarketingParameters(url: URL, custom?: MarketingParameters): WalkerOS.Properties`
184
+ extracts UTM and click ID parameters from URLs.
185
+
186
+ ```js
187
+ getMarketingParameters(
188
+ new URL('https://example.com/?utm_source=docs&gclid=123'),
189
+ );
190
+ // Returns { source: "docs", gclid: "123", clickId: "gclid" }
191
+
192
+ // With custom parameters
193
+ getMarketingParameters(url, { utm_custom: 'custom', partner: 'partnerId' });
194
+ ```
195
+
196
+ ### Type Validation
197
+
198
+ #### Type Checkers
199
+
200
+ A comprehensive set of type checking functions:
201
+
202
+ - `isString(value)`, `isNumber(value)`, `isBoolean(value)`
203
+ - `isArray(value)`, `isObject(value)`, `isFunction(value)`
204
+ - `isDefined(value)`, `isSameType(a, b)`
205
+ - `isPropertyType(value)` - Checks if value is valid walkerOS property
206
+
207
+ #### Property Utilities
208
+
209
+ - `castToProperty(value)` - Casts to valid property type
210
+ - `filterValues(object)` - Filters object to valid properties only
211
+ - `isPropertyType(value)` - Type guard for property validation
212
+
213
+ ### Request Handling
214
+
215
+ #### requestToData
216
+
217
+ `requestToData(parameter: unknown): WalkerOS.AnyObject | undefined` converts
218
+ query strings to JavaScript objects with type casting.
219
+
220
+ ```js
221
+ requestToData('a=1&b=true&c=hello&arr[0]=x&arr[1]=y');
222
+ // Returns { a: 1, b: true, c: 'hello', arr: ['x', 'y'] }
223
+ ```
224
+
225
+ #### requestToParameter
226
+
227
+ `requestToParameter(data: WalkerOS.AnyObject): string` converts objects to
228
+ URL-encoded query strings.
229
+
230
+ ```js
231
+ requestToParameter({ a: 1, b: true, arr: ['x', 'y'] });
232
+ // Returns 'a=1&b=true&arr[0]=x&arr[1]=y'
233
+ ```
234
+
235
+ ### User Agent Parsing
236
+
237
+ #### parseUserAgent
238
+
239
+ `parseUserAgent(userAgent?: string): WalkerOS.User` extracts browser, OS, and
240
+ device information.
241
+
242
+ ```js
243
+ parseUserAgent(navigator.userAgent);
244
+ // Returns { browser: 'Chrome', browserVersion: '91.0', os: 'Windows', ... }
245
+ ```
246
+
247
+ Individual functions are also available:
248
+
249
+ - `getBrowser(userAgent)` - Returns browser name
250
+ - `getBrowserVersion(userAgent)` - Returns browser version
251
+ - `getOS(userAgent)` - Returns operating system
252
+ - `getOSVersion(userAgent)` - Returns OS version
253
+ - `getDeviceType(userAgent)` - Returns 'Desktop', 'Tablet', or 'Mobile'
254
+
255
+ ### Error Handling
256
+
257
+ #### tryCatch
258
+
259
+ `tryCatch(tryFn: Function, catchFn?: Function, finallyFn?: Function)` wraps
260
+ functions with error handling.
261
+
262
+ ```js
263
+ const safeParse = tryCatch(JSON.parse, () => ({}));
264
+ safeParse('{"valid": "json"}'); // Parses successfully
265
+ safeParse('invalid'); // Returns {} instead of throwing
266
+ ```
267
+
268
+ #### tryCatchAsync
269
+
270
+ `tryCatchAsync(tryFn: Function, catchFn?: Function, finallyFn?: Function)` for
271
+ async operations.
272
+
273
+ ```js
274
+ const safeAsyncCall = tryCatchAsync(
275
+ () => fetchUserData(),
276
+ (error) => ({ error: 'Failed to load user' }),
277
+ );
278
+ ```
279
+
280
+ ### Performance Optimization
281
+
282
+ #### debounce
283
+
284
+ `debounce(fn: Function, wait?: number)` delays function execution until after
285
+ the wait time.
286
+
287
+ ```js
288
+ const debouncedSearch = debounce(searchFunction, 300);
289
+ // Only executes after 300ms of inactivity
290
+ ```
291
+
292
+ #### throttle
293
+
294
+ `throttle(fn: Function, wait?: number)` limits function execution frequency.
295
+
296
+ ```js
297
+ const throttledScroll = throttle(scrollHandler, 100);
298
+ // Executes at most every 100ms
299
+ ```
300
+
301
+ ### Utilities
302
+
303
+ #### trim
304
+
305
+ `trim(str: string): string` removes whitespace from string ends.
306
+
307
+ #### throwError
308
+
309
+ `throwError(message: string)` throws descriptive errors.
310
+
311
+ #### onLog
312
+
313
+ `onLog(message: unknown, verbose?: boolean)` provides consistent logging.
314
+
315
+ ```js
316
+ onLog('Debug info', true); // Logs message
317
+ onLog('Silent message'); // No output
318
+ ```
319
+
320
+ ### Validation
321
+
322
+ #### validateEvent
323
+
324
+ `validateEvent(obj: unknown, customContracts?: Schema.Contracts): WalkerOS.Event | never`
325
+ validates event structure and throws on invalid events.
326
+
327
+ #### validateProperty
328
+
329
+ Validates that values conform to walkerOS property types.
330
+
331
+ ## Type Definitions
332
+
333
+ See [src/types/](./src/types/) for TypeScript interfaces:
334
+
335
+ - [event.ts](./src/types/event.ts) - Event structure
336
+ - [destination.ts](./src/types/destination.ts) - Destination interface
337
+ - [source.ts](./src/types/source.ts) - Source interface
338
+ - [mapping.ts](./src/types/mapping.ts) - Mapping configuration
339
+
340
+ ## Related
341
+
342
+ - [Website Documentation](https://www.walkeros.io/docs/)
343
+ - [Collector Package](../collector/) - Event processing engine
344
+ - [Web Core](https://www.walkeros.io/docs/sources/web/) - Browser-specific
345
+ functions
346
+ - [Server Core](https://www.walkeros.io/docs/sources/server/) - Node.js server
347
+ functions
348
+
349
+ ## Contribute
350
+
351
+ Feel free to contribute by submitting an
352
+ [issue](https://github.com/elbwalker/walkerOS/issues), starting a
353
+ [discussion](https://github.com/elbwalker/walkerOS/discussions), or getting in
354
+ [contact](https://calendly.com/elb-alexander/30min).
355
+
356
+ ## License
357
+
358
+ This project is licensed under the MIT License.