Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

API Reference

generate(input, options?)

The main function that transforms flat spreadsheet data into nested JSON.

import { generate } from 'path-binder'

const { result, skipped } = generate(input, {
  schema,              // optional: SchemaObject
  skipScope: 'cell',   // optional: 'cell' | 'row' (default: 'cell')
})

Parameters

ParameterTypeRequiredDescription
inputInputDataYes{ path, value } pair arrays per sheet
options.schemaSchemaObjectNoType casting and filtering definition
options.skipScope'cell' | 'row'NoSkip granularity (default: 'cell')

Returns: GenerateResult

type GenerateResult = {
  readonly result: Record<string, unknown>
  readonly skipped: readonly ParseSkipped[]
}
  • result: Keyed by top-level property, values are always arrays. This is because each spreadsheet row corresponds to one entity.
// Input: 2 rows with user.name = "Taro", user.name = "Jiro"
// Result: { user: [{ name: 'Taro' }, { name: 'Jiro' }] }
//           ↑ user is always an array
  • skipped: Array of entries that could not be processed. See Skip Handling for details.

InputData Format

3-layer structure: Sheet → Rows → Cells

type InputData = {
  readonly [sheetName: string]: readonly (readonly PathValuePair[])[]
}
//                               ↑ array of rows    ↑ array of cells (path + value pairs)

type PathValuePair = {
  readonly path: string
  readonly value: unknown
}

Visualized:

InputData = {
  "Sheet1": [          // ← sheet name
    [                  // ← row 1
      { path, value }, // ← cell 1
      { path, value }, // ← cell 2
    ],
    [                  // ← row 2
      { path, value },
      { path, value },
    ],
  ],
  "Sheet2": [ ... ],   // ← another sheet
}

defineSchema(definition)

Defines a schema object.

import { defineSchema, asString, asNumber } from 'path-binder'

const schema = defineSchema({
  user: {
    name: asString(),
    age: asNumber(),
  },
})

Cast Functions

FunctionSignatureConversion
asString()() => SchemaNodeString(value)
asNumber()() => SchemaNodeNumber(value)
asBoolean()() => SchemaNodeBoolean(value)
asDate()() => SchemaNodenew Date(value)
asCustom(fn)(fn: CastFn) => SchemaNodeCustom function
asAny()(partial?: object) => SchemaNodeNo casting (pass all paths)
arrayOf(schema)(schema: SchemaNode | object) => ArraySchemaArray element schema
type CastFn = (value: unknown) => unknown

Type Exports

import type {
  InputData,         // Input data type
  PathValuePair,     // { path: string, value: unknown }
  GenerateOptions,   // generate() options
  GenerateResult,    // generate() return type
  ParseSkipped,      // Skipped entry
  ParseSkipReason,   // Skip reason code
  SchemaObject,      // defineSchema() argument type
  SchemaNode,        // Each schema node
  CastFn,            // Cast function type
  ArraySchema,       // arrayOf() return type
  AnySchema,         // asAny() return type
} from 'path-binder'