v2.0.0

Object helpers

Tiny, dependency-free object and enum helpers — pick, omit, getEnumValue, and the isArrayOfArray type guard.

Overview

A handful of small, dependency-free helpers used across the SDK for reshaping objects and narrowing types. All four are plain functions exported from @bitrix24/b24jssdk:

  • pick — a new object containing only the listed keys.
  • omit — a shallow copy with the listed keys removed.
  • getEnumValue — resolve a raw string/number to the matching enum member (or undefined).
  • isArrayOfArray — a type guard distinguishing an array of arrays from a flat array.
import { pick, omit } from '@bitrix24/b24jssdk'

const user = { ID: '1', NAME: 'Ann', EMAIL: 'ann@example.com' }

pick(user, ['ID', 'NAME']) // { ID: '1', NAME: 'Ann' }
omit(user, ['EMAIL'])      // { ID: '1', NAME: 'Ann' }

Method Signature

pick<Data extends object, Keys extends keyof Data>(
  data: Data,
  keys: Keys[]
): Pick<Data, Keys>

omit<Data extends object, Keys extends keyof Data>(
  data: Data,
  keys: Keys[]
): Omit<Data, Keys>

getEnumValue<T extends Record<string, string | number>>(
  enumObj: T,
  value: string | number
): T[keyof T] | undefined

isArrayOfArray<A>(item: A[] | A[][]): item is A[][]

Key Concepts

  • pick / omit are shallow. pick copies the chosen property references into a fresh object; omit spreads data into a shallow clone and deletes the listed keys. Nested objects are shared by reference, and the original data is never mutated. Both return a precisely-typed result (Pick<Data, Keys> / Omit<Data, Keys>).
  • getEnumValue is a runtime lookup. It checks whether value is present in Object.values(enumObj) and, if so, returns it narrowed to the enum's member type; otherwise it returns undefined. It works with both string and numeric enums and is the safe way to turn an untrusted REST string into a typed enum member.
  • isArrayOfArray only inspects the first element. It returns Array.isArray(item[0]) — it does not scan the whole array. On an empty array item[0] is undefined, so it returns false. The SDK uses it to tell a single list of batch calls apart from a list-of-lists.

Error Handling

None of these helpers throw. pick / omit operate on a copy and leave the input untouched, getEnumValue returns undefined for a value that is not a member of the enum, and isArrayOfArray returns false for an empty array rather than failing.

Examples

Reshape a REST record before sending it on:

import { pick, omit } from '@bitrix24/b24jssdk'

const contact = { ID: '42', NAME: 'Ann', PHONE: '+100', SECRET: 'x' }

// Keep only what the next call needs
pick(contact, ['ID', 'NAME']) // { ID: '42', NAME: 'Ann' }

// Drop an internal field before logging
omit(contact, ['SECRET']) // { ID: '42', NAME: 'Ann', PHONE: '+100' }

Resolve a raw field type to a DataType member:

import { getEnumValue, DataType } from '@bitrix24/b24jssdk'

getEnumValue(DataType, 'string') // DataType.string
getEnumValue(DataType, 'nope')   // undefined

Branch on array shape with a type guard:

import { isArrayOfArray } from '@bitrix24/b24jssdk'

isArrayOfArray([[1, 2], [3, 4]]) // true  — array of arrays
isArrayOfArray([1, 2, 3])        // false — flat array
isArrayOfArray([])               // false — empty array

Alternatives and Recommendations

  • Use getEnumValue with the enums in types/common. It pairs naturally with DataType and any other SDK enum when you need to validate a raw REST string before treating it as a typed value.
  • Prefer pick / omit for small, explicit reshaping. They are shallow by design; for deep cloning or complex transforms reach for a dedicated utility rather than layering these.
  • isArrayOfArray is a heuristic, not a validator. It trusts the first element to represent the whole array — do not use it to validate mixed or untrusted input where later elements may differ in shape.
  • Runtime type guards for scalar values live in Type. isArrayOfArray answers one narrow question about array nesting; use Type's guards (isArrayFilled, isStringFilled, …) for general shape checks.