๐Ÿ“ฆ colinhacks / trailhead

Find paths through your data in a type-safe way

โ˜… 13 stars โ‘‚ 1 forks ๐Ÿ‘ 13 watching โš–๏ธ MIT License
typescript
๐Ÿ“ฅ Clone https://github.com/colinhacks/trailhead.git
HTTPS git clone https://github.com/colinhacks/trailhead.git
SSH git clone git@github.com:colinhacks/trailhead.git
CLI gh repo clone colinhacks/trailhead
Colin McDonnell Colin McDonnell Fixed usage example b7a6f98 5 years ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ src
๐Ÿ“„ .eslintrc
๐Ÿ“„ .gitignore
๐Ÿ“„ .prettierrc.json
๐Ÿ“„ jestconfig.json
๐Ÿ“„ LICENSE
๐Ÿ“„ package.json
๐Ÿ“„ README.md
๐Ÿ“„ tsconfig.json
๐Ÿ“„ yarn-error.log
๐Ÿ“„ yarn.lock
๐Ÿ“„ README.md

Trailhead

A utility for generating typesafe paths through your data



Compatibility

Requires Typescript 4.0+ and the Proxy API. You can check browser compatibility for Proxies at caniuse.com/proxy.

Usage

import { trailhead } from 'trailhead';

type Data = {
  firstName: string;
  array: string[];
  nest: {
    nest: {
      firstName: string;
    };
  };
  arrayObject: {
    firstName: string;
    lastName: string;
  }[];
};

const test = trailhead<Data>();

console.log(test); // => { __path: [] }
console.log(test.firstName); // => { __path: ["firstName"] }
console.log(test.array[5]); // => { __path: ["array", 5] }
console.log(test.nest.nest.firstName); // => { __path: ["nest", "nest", "firstName"] }
console.log(test.arrayObject); // => { __path: ["arrayObject"] }
console.log(test.arrayObject[5]); // => { __path: ["arrayObject", 5, "firstName"] }
console.log(test.arrayObject[5].firstName); // => { __path: ["arrayObject", 5, "firstName"] }