All Projects → bsalex → Typed Path

bsalex / Typed Path

Licence: other
Type safe object field string paths for typescript.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typed Path

Set Value
Set nested values on an object using dot-notation, like 'a.b.c'.
Stars: ✭ 203 (+178.08%)
Mutual labels:  path, lodash
Morphi
Shapes for SwiftUI ♡☾
Stars: ✭ 54 (-26.03%)
Mutual labels:  path
Macsvg
macSVG - An open-source macOS app for designing HTML5 SVG (Scalable Vector Graphics) art and animation with a WebKit web view ➤➤➤
Stars: ✭ 789 (+980.82%)
Mutual labels:  path
Resolve Dir
Resolve a directory that is either local, global or in the user's home directory.
Stars: ✭ 14 (-80.82%)
Mutual labels:  path
Logging Helpers
Basic template helpers for printing messages out to the console. Useful for debugging context in templates. Should work with any template engine.
Stars: ✭ 5 (-93.15%)
Mutual labels:  lodash
Handle Path Oz
Android Library to handle multiple Uri's(paths) received through Intents.
Stars: ✭ 36 (-50.68%)
Mutual labels:  path
Styled Tools
Useful interpolated functions for CSS-in-JS
Stars: ✭ 761 (+942.47%)
Mutual labels:  lodash
Smoldash
Smoldash, A tiny lodash alternative built for the modern web
Stars: ✭ 66 (-9.59%)
Mutual labels:  lodash
Rambda
Faster and smaller alternative to Ramda
Stars: ✭ 1,066 (+1360.27%)
Mutual labels:  lodash
Three.js Pathtracing Renderer
Real-time PathTracing with global illumination and progressive rendering, all on top of the Three.js WebGL framework. Click here for Live Demo: https://erichlof.github.io/THREE.js-PathTracing-Renderer/Geometry_Showcase.html
Stars: ✭ 872 (+1094.52%)
Mutual labels:  path
Javascript ninja
javascript-ninja 😆
Stars: ✭ 11 (-84.93%)
Mutual labels:  lodash
Mithril Data
A rich data model library for Mithril javascript framework
Stars: ✭ 17 (-76.71%)
Mutual labels:  lodash
Graphql Lodash
🛠 Data manipulation for GraphQL queries with lodash syntax
Stars: ✭ 1,003 (+1273.97%)
Mutual labels:  lodash
Sensei Grid
Simple and lightweight data grid in JS/HTML
Stars: ✭ 808 (+1006.85%)
Mutual labels:  lodash
Lodash Webpack Plugin
Smaller modular Lodash builds.
Stars: ✭ 1,106 (+1415.07%)
Mutual labels:  lodash
Etree
parse and generate XML easily in go
Stars: ✭ 763 (+945.21%)
Mutual labels:  path
Path.swift
Delightful, robust, cross-platform and chainable file-pathing functions.
Stars: ✭ 839 (+1049.32%)
Mutual labels:  path
Emptyd Admin Webpack
基于typescript react webpack的脚手架
Stars: ✭ 30 (-58.9%)
Mutual labels:  lodash
Pasition
Path Transition with little JS code, render to anywhere - 轻量级 Path 过渡库,渲染到任何地方
Stars: ✭ 1,149 (+1473.97%)
Mutual labels:  path
Wslpath
wslpath - Converts Unix and Windows format paths in WSL
Stars: ✭ 64 (-12.33%)
Mutual labels:  path

Typed Path

https://nodei.co/npm/typed-path.svg?downloads=true&downloadRank=true&stars=true

Travis Hits Contributions welcome GitHub top language David npm bundle size GitHub last commit Snyk Vulnerabilities for GitHub Repo GitHub issues Code Climate maintainability Code Climate technical debt codecov

Overview

This small utility helps to extract type information from a TypeScript class, interface or type to use it in your code.

Example:

import {typedPath} from 'typed-path';

type TestType = {
    a: {
        testFunc: () => {result: string};
        b: {
            arrayOfArrays: string[][];
            c: {
                d: number;
            };
        }[];
    };
};

console.log(typedPath<TestType>().a.b[5].c.d.$rawPath);
/*
Outputs
["a", "b", 5, "c", "d"]

*/

Please see other path access methods and how to add custom path access methods below.

The utility might also be used to add type protection to such methods as _.get, _.map, _.set, R.pluck from libraries like lodash, ramda.

It is recommended, though, to use optional chaining instead.


Features

Errors

With typed-path, typescript can check paths and warns you about errors.

Path access methods

Default

.$path

@m-abboud
Also, you can get access to the path string using $path special field.

Like this:

    console.log(tp<TestType>().a.b.c.d.$path); // this will output "a.b.c.d"
.$raw

@dcbrwn
If you need a raw path, which is of type string[] - you can get it using $raw special field.
Deprecated, since it transforms symbols and numbers to strings, which might be not an expected behavior (the method name is "raw"). Please use .$rawPath

    console.log(tp<TestType>().a.b.c.d.$raw); // this will output ["a", "b", "c", "d"]
.$rawPath

If you need a raw path, which is of type (string | number | Symbol)[] - you can get it using $rawPath special field.

    console.log(tp<TestType>().a.b[5].c.d.$rawPath); // this will output ["a", "b", 5, "c", "d"]

The $rawPath is something that you might want to use with the following methods from Ramda, to add type safety on the path:

Example: https://codesandbox.io/s/typed-path-ramda-assoc-path-x3qby?file=/src/index.ts

Additional handlers

@nick-lvov-dev

You can extend path handlers functionality using additional handlers:

const testAdditionalHandlers = {
    $url: (path: TypedPathKey[]) => path.join('/')
}

console.log(tp<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers).a.b.c.$url); // this will output "a/b/c"

The additional handlers are also chainable:

const testAdditionalHandlers = {
    $abs: (path: TypedPathKey[]) => typedPath<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers, ['', ...path]),
    $url: (path: TypedPathKey[]) => path.join('/'),
    $length: (path: TypedPathKey[]) => path.length
}

console.log(tp<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers).a.b.c.$abs.$url); // this will output "/a/b/c"

Suggestions

Also, typed-path allows typescript to suggest field names for you.

License

Copyright (c) 2021 Oleksandr Beshchuk <[email protected]>
Licensed under the Apache License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].