1.0.5 • Published 9 months ago

@json-walker/core v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@json-walker/core

Core implementation of the walker

Usage

Walker usage

function grabProperties(value) {
  const properties = [];
  const walker = new Walker(value);
  let optionalWalkerMetadata;

  do {
    optionalWalkerMetadata = walker.nextStep();

    if (optionalWalkerMetadata.isSome()) {
      properties.push({
        path: optionalWalkerMetadata.value.propertyPath.toString(),
        type: optionalWalkerMetadata.value.propertyType,
        value: optionalWalkerMetadata.value.propertyValue,
      });
    }
  } while (optionalWalkerMetadata.isSome());

  return properties;
}

const secondLevel = { label: 'foo' };
const array = [secondLevel];
const firstLevel = { records: array };
const actual = grabProperties(firstLevel);

/* actual content:
 * [
    {
      path: 'records',
      type: 'array',
      value: [{ label: 'foo' }],
    },
    {
      path: 'records[0]',
      type: 'object',
      value: { label: 'foo' },
    },
    {
      path: 'records[0].label',
      type: 'string',
      value: 'foo',
    },
  ]
 */

IterableWalker usage

function grabProperties(value) {
  const properties = [];
  const walker = new IterableWalker(value);

  for (const value of walker) {
    properties.push({
      path: value.propertyPath.toString(),
      type: value.propertyType,
      value: value.propertyValue,
    });
  }

  return properties;
}

const secondLevel = { label: 'foo' };
const array = [secondLevel];
const firstLevel = { records: array };
const actual = grabProperties(firstLevel);

/* actual content:
 * [
    {
      path: 'records',
      type: 'array',
      value: [{ label: 'foo' }],
    },
    {
      path: 'records[0]',
      type: 'object',
      value: { label: 'foo' },
    },
    {
      path: 'records[0].label',
      type: 'string',
      value: 'foo',
    },
  ]
 */

Commands

  • npm run dev:linting: Lint files
  • npm test: Run tests
  • npm run test:coverage: Run tests and see coverage reports

Contributing

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

10 months ago

1.0.1

11 months ago

1.0.0

11 months ago