| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4c6f73c commit 94c720c
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ const { | |||
| 41 | 41 | StringPrototypeToUpperCase, | |
| 42 | 42 | Symbol, | |
| 43 | 43 | SymbolFor, | |
| 44 | + SymbolPrototypeGetDescription, | ||
| 44 | 45 | SymbolReplace, | |
| 45 | 46 | SymbolSplit, | |
| 46 | 47 | } = primordials; | |
@@ -906,10 +907,37 @@ const encodingsMap = { __proto__: null }; | |||
| 906 | 907 | for (let i = 0; i < encodings.length; ++i) | |
| 907 | 908 | encodingsMap[encodings[i]] = i; | |
| 908 | 909 | ||
| 910 | + /** | ||
| 911 | + * Reassigns the .name property of a function. | ||
| 912 | + * Should be used when function can't be initially defined with desired name | ||
| 913 | + * or when desired name should include `#`, `[`, `]`, etc. | ||
| 914 | + * @param {string | symbol} name | ||
| 915 | + * @param {Function} fn | ||
| 916 | + * @param {object} [descriptor] | ||
| 917 | + * @returns {Function} the same function, renamed | ||
| 918 | + */ | ||
| 919 | + function assignFunctionName(name, fn, descriptor = kEmptyObject) { | ||
| 920 | + if (typeof name !== 'string') { | ||
| 921 | + const symbolDescription = SymbolPrototypeGetDescription(name); | ||
| 922 | + assert(symbolDescription !== undefined, 'Attempted to name function after descriptionless Symbol'); | ||
| 923 | + name = `[${symbolDescription}]`; | ||
| 924 | + } | ||
| 925 | + return ObjectDefineProperty(fn, 'name', { | ||
| 926 | + __proto__: null, | ||
| 927 | + writable: false, | ||
| 928 | + enumerable: false, | ||
| 929 | + configurable: true, | ||
| 930 | + ...ObjectGetOwnPropertyDescriptor(fn, 'name'), | ||
| 931 | + ...descriptor, | ||
| 932 | + value: name, | ||
| 933 | + }); | ||
| 934 | + } | ||
| 935 | + | ||
| 909 | 936 | module.exports = { | |
| 910 | 937 | getLazy, | |
| 911 | 938 | assertCrypto, | |
| 912 | 939 | assertTypeScript, | |
| 940 | + assignFunctionName, | ||
| 913 | 941 | cachedResult, | |
| 914 | 942 | convertToValidSignal, | |
| 915 | 943 | createClassWrapper, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | require('../common'); | |
| 5 | 5 | const assert = require('assert'); | |
| 6 | 6 | const { types } = require('util'); | |
| 7 | - const { isError } = require('internal/util'); | ||
| 7 | + const { assignFunctionName, isError } = require('internal/util'); | ||
| 8 | 8 | const vm = require('vm'); | |
| 9 | 9 | ||
| 10 | 10 | // Special cased errors. Test the internal function which is used in | |
@@ -35,3 +35,30 @@ const vm = require('vm'); | |||
| 35 | 35 | assert(!(differentRealmErr instanceof Error)); | |
| 36 | 36 | assert(isError(differentRealmErr)); | |
| 37 | 37 | } | |
| 38 | + | ||
| 39 | + { | ||
| 40 | + const nameMap = new Map([ | ||
| 41 | + [ 'meaningfulName', 'meaningfulName' ], | ||
| 42 | + [ '', '' ], | ||
| 43 | + [ Symbol.asyncIterator, '[Symbol.asyncIterator]' ], | ||
| 44 | + [ Symbol('notWellKnownSymbol'), '[notWellKnownSymbol]' ], | ||
| 45 | + ]); | ||
| 46 | + for (const fn of [ | ||
| 47 | + () => {}, | ||
| 48 | + function() {}, | ||
| 49 | + function value() {}, | ||
| 50 | + ({ value() {} }).value, | ||
| 51 | + new Function(), | ||
| 52 | + Function(), | ||
| 53 | + function() {}.bind(null), | ||
| 54 | + class {}, | ||
| 55 | + class value {}, | ||
| 56 | + ]) { | ||
| 57 | + for (const [ stringOrSymbol, expectedName ] of nameMap) { | ||
| 58 | + const namedFn = assignFunctionName(stringOrSymbol, fn); | ||
| 59 | + assert.strictEqual(fn, namedFn); | ||
| 60 | + assert.strictEqual(namedFn.name, expectedName); | ||
| 61 | + assert.strictEqual(namedFn.bind(null).name, `bound ${expectedName}`); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments