ts-nameof

  • Version 5.0.0
  • Published
  • 18.9 kB
  • 3 dependencies
  • MIT license

Install

npm i ts-nameof
yarn add ts-nameof
pnpm add ts-nameof

Overview

nameof in TypeScript

Index

Functions

function nameof

nameof: typeof nameof;
  • Gets a string representation of the last identifier of the given expression.

    Parameter obj

    An expression for which the last identifier will be parsed.

    Example 1

    nameof(console) -> "console"

    Example 2

    nameof(console.log) -> "log"

    Example 3

    nameof(console["warn"]) -> "warn"

Namespaces

namespace nameof

namespace nameof {}

    function full

    full: {
    <T>(periodIndex?: number): string;
    <T>(func: (obj: T) => any, periodIndex?: number): string;
    (obj: any, periodIndex?: number): string;
    };
    • Gets the string representation of the entire type parameter expression.

      Parameter periodIndex

      Specifies the index of the part of the expression to parse. When absent, the full expression will be parsed. A negative index can be used, indicating an offset from the end of the sequence.

      Example 1

      nameof.full<MyNamespace.MyInnerInterface>() -> "MyNamespace.MyInnerInterface"

      Example 2

      nameof.full<MyNamespace.MyInnerInterface>(1) -> "MyInnerInterface"

      Example 3

      nameof.full<Array>() -> "Array"

      Example 4

      nameof.full<MyNamespace.AnotherNamespace.MyInnerInterface>>(-1) -> "MyInnerInterface"

    • Gets the string representation of the entire resultant expression.

      Parameter func

      A function for which the result will be parsed, excluding the parameter's identifier.

      Parameter periodIndex

      Specifies the index of the part of the expression to parse. When absent, the full expression will be parsed. A negative index can be used, indicating an offset from the end of the sequence.

      Example 1

      nameof.full(o => o.prop.prop2) -> "prop.prop2"

      Example 2

      nameof.full(o => o.prop.prop2.prop3, 1) -> "prop2.prop3"

      Example 3

      nameof.full(o => o.prop.prop2.prop3, -1) -> `"prop3"

    • Gets the string representation of the entire given expression.

      Parameter obj

      The expression which will be parsed.

      Parameter periodIndex

      Specifies the index of the part of the expression to parse. When absent, the full expression will be parsed. A negative index can be used, indicating an offset from the end of the sequence.

      Example 1

      nameof.full(console.log) -> "console.log"

      Example 2

      nameof.full(window.alert.length, -1) -> "length"

      Example 3

      nameof.full(window.alert.length, 2) -> "length"

    function interpolate

    interpolate: <T>(value: T) => T;
    • Embeds an expression into the string representation of the result of nameof.full.

      Parameter value

      The value to interpolate.

      Example 1

      nameof.full(myObj.prop[nameof.interpolate(i)]) -> myObj.prop[${i}]

    function split

    split: {
    <T>(func: (obj: T) => any, periodIndex?: number): string[];
    (obj: any, periodIndex?: number): string[];
    };
    • Gets an array of strings where each element is a subsequent part of the expression provided.

      Parameter func

      A function for which the resultant parts will be parsed, excluding the parameter's identifier.

      Parameter periodIndex

      Specifies the index of the part of the expression to parse. When absent, the full expression will be parsed. A negative index can be used, indicating an offset from the end of the sequence.

      Example 1

      nameof.split(o => o.prop.prop2.prop3) -> ["prop", "prop2", "prop3"]

      Example 2

      nameof.split(o => o.prop.prop2.prop3, 1) -> ["prop2", "prop3"]

      Example 3

      nameof.split(o => o.prop.prop2.prop3, -1) -> ["prop", "prop2"]

    • Gets an array of strings where each element is a subsequent part of the expression provided.

      Parameter obj

      An expression for which the parts will be parsed.

      Parameter periodIndex

      Specifies the index of the part of the expression to parse. When absent, the full expression will be parsed. A negative index can be used, indicating an offset from the end of the sequence.

      Example 1

      nameof.split(myObj.prop.prop2.prop3) -> ["myObj", "prop", "prop2", "prop3"]

      Example 2

      nameof.split(myObj.prop.prop2.prop3, -3);, ["prop", "prop2", "prop3"];

      Example 3

      nameof.split(myObj.prop.prop2.prop3, 2);, ["prop2", "prop3"]

    function toArray

    toArray: { <T>(func: (obj: T) => any[]): string[]; (...args: any[]): string[] };
    • Gets an array containing the string representation of the final identifier of each expression in the array returned by the provided function.

      Parameter func

      A function returning an array of expressions to be parsed, excluding the parameter's identifier.

      Example 1

      nameof.toArray(o => [o.firstProp, o.otherProp.secondProp, o.other]) -> ["firstProp", "secondProp", "other"]

      Example 2

      nameof.toArray(o => [o.prop, nameof.full(o.myProp.otherProp, 1)]) -> ["prop", "myProp.otherProp"]

    • Gets an array containing the string representation of each expression in the arguments.

      Parameter args

      An array of expressions to be parsed.

      Example 1

      nameof.toArray(myObject, otherObject) -> ["myObject", "otherObject"]

      Example 2

      nameof.toArray(obj.firstProp, obj.secondProp, otherObject, nameof.full(obj.other)) -> ["firstProp", "secondProp", "otherObject", "obj.other"]

    namespace ts-nameof

    module 'ts-nameof' {}

      variable api

      const api: Api;

        Package Files (1)

        Dependencies (3)

        Dev Dependencies (13)

        Peer Dependencies (1)

        Badge

        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/ts-nameof.

        • Markdown
          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/ts-nameof)
        • HTML
          <a href="https://www.jsdocs.io/package/ts-nameof"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>