@twin.org/nameof 0.0.2-next.9 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/examples.md CHANGED
@@ -1,74 +1,60 @@
1
- # @twin.org/nameof - Examples
1
+ # Nameof Examples
2
2
 
3
- ## `nameof<T>`
3
+ Use these snippets for strongly typed name extraction patterns that remain safe during refactors.
4
4
 
5
- One of the internal transformers in the TypeScript compiler will reduce an interface to no code, which means that at runtime the type information is missing. So operations where you might like to find the name of a class/type etc are not available.
6
-
7
- We have many places in our code where we want to use the name of the class, we could do the following.
5
+ ## `nameof`
8
6
 
9
7
  ```typescript
10
- export class MyClass {
11
- public static className(): string {
12
- return 'MyClass';
13
- }
14
- }
15
- console.log(MyClass.className()); // Outputs "MyClass"
16
- ```
8
+ import { nameof } from '@twin.org/nameof';
17
9
 
18
- But if you changed the name of the class you would have to make sure you also replaced all string instances where it was mentioned as well, this is prone to lots of errors.
10
+ class AccountState {
11
+ public id!: string;
12
+ }
19
13
 
20
- We can use the `nameof` transformer as follows.
14
+ nameof<AccountState>(); // 'AccountState'
15
+ ```
21
16
 
22
17
  ```typescript
23
18
  import { nameof } from '@twin.org/nameof';
24
19
 
25
- export class MyClass {
26
- public static className(): string {
27
- return nameof<MyClass>();
28
- }
20
+ interface Profile {
21
+ displayName: string;
22
+ stats?: {
23
+ loginCount: number;
24
+ };
29
25
  }
30
- console.log(MyClass.className()); // Outputs "MyClass"
31
- ```
32
-
33
- Now if we rename `MyClass` without updating the references in the `nameof` call we will get a compiler error, code auto refactoring would actually perform the rename for us.
34
26
 
35
- Since `nameof` is performed as a compile time transform, the actual code produced looks like this.
27
+ const profile: Profile = {
28
+ displayName: 'Ari',
29
+ stats: {
30
+ loginCount: 12
31
+ }
32
+ };
36
33
 
37
- ```javascript
38
- export class MyClass {
39
- static public className(): string {
40
- return "MyClass";
41
- }
42
- }
43
- console.log(MyClass.className()); // Outputs "MyClass"
34
+ nameof(profile.displayName); // 'displayName'
35
+ nameof(profile.stats?.loginCount); // 'stats.loginCount'
44
36
  ```
45
37
 
46
- This means there is no additional overheads in function calls at runtime.
47
-
48
- ## `nameof(propName)`
49
-
50
- The `nameof(propName)` transform behaves in much the same way as `nameof<T>`, but instead works on properties passed to it.
38
+ ## `nameofKebabCase`
51
39
 
52
40
  ```typescript
53
- import { nameof } from '@twin.org/nameof';
41
+ import { nameofKebabCase } from '@twin.org/nameof';
54
42
 
55
- export class MyClass {
56
- public static propName(aProp: string): string {
57
- return nameof(aProp);
58
- }
43
+ class ReportSummary {
44
+ public totalCount!: number;
59
45
  }
60
- console.log(MyClass.propName()); // Outputs "aProp"
46
+
47
+ nameofKebabCase<ReportSummary>(); // 'report-summary'
61
48
  ```
62
49
 
63
- You can also used it with chained properties, nullish operators can also be used, but are removed.
50
+ ## `nameofCamelCase`
64
51
 
65
52
  ```typescript
66
- import { nameof } from '@twin.org/nameof';
53
+ import { nameofCamelCase } from '@twin.org/nameof';
67
54
 
68
- export class MyClass {
69
- public static propName(aProp: { inner?: boolean }): string {
70
- return nameof(aProp?.inner);
71
- }
55
+ class UserProfileRecord {
56
+ public profileImageUrl!: string;
72
57
  }
73
- console.log(MyClass.propName()); // Outputs "aProp.inner"
58
+
59
+ nameofCamelCase<UserProfileRecord>(); // 'userProfileRecord'
74
60
  ```
@@ -0,0 +1,31 @@
1
+ # Function: nameofCamelCase()
2
+
3
+ > **nameofCamelCase**\<`T`\>(`property?`, `replaceParent?`): `string`
4
+
5
+ Placeholder method which substitutes the type name as a string at runtime.
6
+
7
+ ## Type Parameters
8
+
9
+ ### T
10
+
11
+ `T`
12
+
13
+ ## Parameters
14
+
15
+ ### property?
16
+
17
+ `unknown`
18
+
19
+ The property to get the name of.
20
+
21
+ ### replaceParent?
22
+
23
+ `string`
24
+
25
+ Optional object name to replace the top level object of a property path.
26
+
27
+ ## Returns
28
+
29
+ `string`
30
+
31
+ The type name as a string.
@@ -0,0 +1,31 @@
1
+ # Function: nameofKebabCase()
2
+
3
+ > **nameofKebabCase**\<`T`\>(`property?`, `replaceParent?`): `string`
4
+
5
+ Placeholder method which substitutes the type name as a string at runtime.
6
+
7
+ ## Type Parameters
8
+
9
+ ### T
10
+
11
+ `T`
12
+
13
+ ## Parameters
14
+
15
+ ### property?
16
+
17
+ `unknown`
18
+
19
+ The property to get the name of.
20
+
21
+ ### replaceParent?
22
+
23
+ `string`
24
+
25
+ Optional object name to replace the top level object of a property path.
26
+
27
+ ## Returns
28
+
29
+ `string`
30
+
31
+ The type name as a string.
@@ -3,3 +3,5 @@
3
3
  ## Functions
4
4
 
5
5
  - [nameof](functions/nameof.md)
6
+ - [nameofKebabCase](functions/nameofKebabCase.md)
7
+ - [nameofCamelCase](functions/nameofCamelCase.md)
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@twin.org/nameof",
3
- "version": "0.0.2-next.9",
4
- "description": "Provides the definitions for the methods which are processed by the nameof-transformer",
3
+ "version": "0.0.2",
4
+ "description": "The definitions for the methods which are processed by the nameof-transformer",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "git+https://github.com/twinfoundation/framework.git",
7
+ "url": "git+https://github.com/iotaledger/framework.git",
8
8
  "directory": "packages/nameof"
9
9
  },
10
10
  "author": "martyn.janes@iota.org",
@@ -13,21 +13,30 @@
13
13
  "engines": {
14
14
  "node": ">=20.0.0"
15
15
  },
16
- "main": "./dist/cjs/index.cjs",
17
- "module": "./dist/esm/index.mjs",
16
+ "main": "./dist/es/index.js",
18
17
  "types": "./dist/types/index.d.ts",
19
18
  "exports": {
20
19
  ".": {
21
20
  "types": "./dist/types/index.d.ts",
22
- "require": "./dist/cjs/index.cjs",
23
- "import": "./dist/esm/index.mjs"
21
+ "import": "./dist/es/index.js",
22
+ "default": "./dist/es/index.js"
24
23
  }
25
24
  },
26
25
  "files": [
27
- "dist/cjs",
28
- "dist/esm",
26
+ "dist/es",
29
27
  "dist/types",
30
28
  "locales",
31
29
  "docs"
32
- ]
30
+ ],
31
+ "keywords": [
32
+ "twin",
33
+ "trade",
34
+ "iota",
35
+ "framework",
36
+ "blockchain"
37
+ ],
38
+ "bugs": {
39
+ "url": "git+https://github.com/iotaledger/framework/issues"
40
+ },
41
+ "homepage": "https://twindev.org"
33
42
  }
@@ -1,16 +0,0 @@
1
- 'use strict';
2
-
3
- // Copyright 2024 IOTA Stiftung.
4
- // SPDX-License-Identifier: Apache-2.0.
5
- /**
6
- * Placeholder method which substitutes the type name as a string at runtime.
7
- * @param property The property to get the name of.
8
- * @param replaceParent Optional object name to replace the top level object of a property path.
9
- * @returns The type name as a string.
10
- */
11
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
12
- function nameof(property, replaceParent) {
13
- return "@twin.org/nameof-transformer is not in the build pipeline, you need to use a compiler that supports transformer plugins.";
14
- }
15
-
16
- exports.nameof = nameof;
@@ -1,14 +0,0 @@
1
- // Copyright 2024 IOTA Stiftung.
2
- // SPDX-License-Identifier: Apache-2.0.
3
- /**
4
- * Placeholder method which substitutes the type name as a string at runtime.
5
- * @param property The property to get the name of.
6
- * @param replaceParent Optional object name to replace the top level object of a property path.
7
- * @returns The type name as a string.
8
- */
9
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
- function nameof(property, replaceParent) {
11
- return "@twin.org/nameof-transformer is not in the build pipeline, you need to use a compiler that supports transformer plugins.";
12
- }
13
-
14
- export { nameof };