| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1fc6fd6 commit f5ff88b
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -579,7 +579,7 @@ changes: | |||
| 579 | 579 | codes. Colors are customizable. See [Customizing `util.inspect` colors][]. | |
| 580 | 580 | **Default:** `false`. | |
| 581 | 581 | * `customInspect` {boolean} If `false`, | |
| 582 | - `[util.inspect.custom](depth, opts)` functions are not invoked. | ||
| 582 | + `[util.inspect.custom](depth, opts, inspect)` functions are not invoked. | ||
| 583 | 583 | **Default:** `true`. | |
| 584 | 584 | * `showProxy` {boolean} If `true`, `Proxy` inspection includes | |
| 585 | 585 | the [`target` and `handler`][] objects. **Default:** `false`. | |
@@ -874,10 +874,18 @@ ignored, if not supported. | |||
| 874 | 874 | ||
| 875 | 875 | <!-- type=misc --> | |
| 876 | 876 | ||
| 877 | + <!-- YAML | ||
| 878 | + added: v0.1.97 | ||
| 879 | + changes: | ||
| 880 | + - version: REPLACEME | ||
| 881 | + pr-url: https://github.com/nodejs/node/pull/41019 | ||
| 882 | + description: The inspect argument is added for more interoperability. | ||
| 883 | + --> | ||
| 884 | + | ||
| 877 | 885 | Objects may also define their own | |
| 878 | - [`[util.inspect.custom](depth, opts)`][util.inspect.custom] function, | ||
| 886 | + [`[util.inspect.custom](depth, opts, inspect)`][util.inspect.custom] function, | ||
| 879 | 887 | which `util.inspect()` will invoke and use the result of when inspecting | |
| 880 | - the object: | ||
| 888 | + the object. | ||
| 881 | 889 | ||
| 882 | 890 | ```js | |
| 883 | 891 | const util = require('util'); | |
@@ -887,7 +895,7 @@ class Box { | |||
| 887 | 895 | this.value = value; | |
| 888 | 896 | } | |
| 889 | 897 | ||
| 890 | - [util.inspect.custom](depth, options) { | ||
| 898 | + [util.inspect.custom](depth, options, inspect) { | ||
| 891 | 899 | if (depth < 0) { | |
| 892 | 900 | return options.stylize('[Box]', 'special'); | |
| 893 | 901 | } | |
@@ -898,8 +906,8 @@ class Box { | |||
| 898 | 906 | ||
| 899 | 907 | // Five space padding because that's the size of "Box< ". | |
| 900 | 908 | const padding = ' '.repeat(5); | |
| 901 | - const inner = util.inspect(this.value, newOptions) | ||
| 902 | - .replace(/\n/g, `\n${padding}`); | ||
| 909 | + const inner = inspect(this.value, newOptions) | ||
| 910 | + .replace(/\n/g, `\n${padding}`); | ||
| 903 | 911 | return `${options.stylize('Box', 'special')}< ${inner} >`; | |
| 904 | 912 | } | |
| 905 | 913 | } | |
@@ -910,9 +918,9 @@ util.inspect(box); | |||
| 910 | 918 | // Returns: "Box< true >" | |
| 911 | 919 | ``` | |
| 912 | 920 | ||
| 913 | - Custom `[util.inspect.custom](depth, opts)` functions typically return a string | ||
| 914 | - but may return a value of any type that will be formatted accordingly by | ||
| 915 | - `util.inspect()`. | ||
| 921 | + Custom `[util.inspect.custom](depth, opts, inspect)` functions typically return | ||
| 922 | + a string but may return a value of any type that will be formatted accordingly | ||
| 923 | + by `util.inspect()`. | ||
| 916 | 924 | ||
| 917 | 925 | ```js | |
| 918 | 926 | const util = require('util'); | |
@@ -942,8 +950,13 @@ In addition to being accessible through `util.inspect.custom`, this | |||
| 942 | 950 | symbol is [registered globally][global symbol registry] and can be | |
| 943 | 951 | accessed in any environment as `Symbol.for('nodejs.util.inspect.custom')`. | |
| 944 | 952 | ||
| 953 | + Using this allows code to be written in a portable fashion, so that the custom | ||
| 954 | + inspect function is used in an Node.js environment and ignored in the browser. | ||
| 955 | + The `util.inspect()` function itself is passed as third argument to the custom | ||
| 956 | + inspect function to allow further portability. | ||
| 957 | + | ||
| 945 | 958 | ```js | |
| 946 | - const inspect = Symbol.for('nodejs.util.inspect.custom'); | ||
| 959 | + const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom'); | ||
| 947 | 960 | ||
| 948 | 961 | class Password { | |
| 949 | 962 | constructor(value) { | |
@@ -954,7 +967,7 @@ class Password { | |||
| 954 | 967 | return 'xxxxxxxx'; | |
| 955 | 968 | } | |
| 956 | 969 | ||
| 957 | - [inspect]() { | ||
| 970 | + [customInspectSymbol](depth, inspectOptions, inspect) { | ||
| 958 | 971 | return `Password <${this.toString()}>`; | |
| 959 | 972 | } | |
| 960 | 973 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -778,7 +778,12 @@ function formatValue(ctx, value, recurseTimes, typedArray) { | |||
| 778 | 778 | const isCrossContext = | |
| 779 | 779 | proxy !== undefined || !(context instanceof Object); | |
| 780 | 780 | const ret = FunctionPrototypeCall( | |
| 781 | - maybeCustom, context, depth, getUserOptions(ctx, isCrossContext)); | ||
| 781 | + maybeCustom, | ||
| 782 | + context, | ||
| 783 | + depth, | ||
| 784 | + getUserOptions(ctx, isCrossContext), | ||
| 785 | + inspect | ||
| 786 | + ); | ||
| 782 | 787 | // If the custom inspection method returned `this`, don't go into | |
| 783 | 788 | // infinite recursion. | |
| 784 | 789 | if (ret !== context) { | |
@@ -1143,7 +1148,7 @@ function getClassBase(value, constructor, tag) { | |||
| 1143 | 1148 | ||
| 1144 | 1149 | function getFunctionBase(value, constructor, tag) { | |
| 1145 | 1150 | const stringified = FunctionPrototypeToString(value); | |
| 1146 | - if (stringified.slice(0, 5) === 'class' && stringified.endsWith('}')) { | ||
| 1151 | + if (stringified.startsWith('class') && stringified.endsWith('}')) { | ||
| 1147 | 1152 | const slice = stringified.slice(5, -1); | |
| 1148 | 1153 | const bracketIndex = slice.indexOf('{'); | |
| 1149 | 1154 | if (bracketIndex !== -1 && | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -974,7 +974,7 @@ util.inspect({ hasOwnProperty: null }); | |||
| 974 | 974 | ||
| 975 | 975 | assert.strictEqual(util.inspect(subject), "{ foo: 'bar' }"); | |
| 976 | 976 | ||
| 977 | - subject[util.inspect.custom] = common.mustCall((depth, opts) => { | ||
| 977 | + subject[util.inspect.custom] = common.mustCall((depth, opts, inspect) => { | ||
| 978 | 978 | const clone = { ...opts }; | |
| 979 | 979 | // This might change at some point but for now we keep the stylize function. | |
| 980 | 980 | // The function should either be documented or an alternative should be | |
@@ -984,12 +984,13 @@ util.inspect({ hasOwnProperty: null }); | |||
| 984 | 984 | assert.strictEqual(opts.budget, undefined); | |
| 985 | 985 | assert.strictEqual(opts.indentationLvl, undefined); | |
| 986 | 986 | assert.strictEqual(opts.showHidden, false); | |
| 987 | + assert.strictEqual(inspect, util.inspect); | ||
| 987 | 988 | assert.deepStrictEqual( | |
| 988 | - new Set(Object.keys(util.inspect.defaultOptions).concat(['stylize'])), | ||
| 989 | + new Set(Object.keys(inspect.defaultOptions).concat(['stylize'])), | ||
| 989 | 990 | new Set(Object.keys(opts)) | |
| 990 | 991 | ); | |
| 991 | 992 | opts.showHidden = true; | |
| 992 | - return { [util.inspect.custom]: common.mustCall((depth, opts2) => { | ||
| 993 | + return { [inspect.custom]: common.mustCall((depth, opts2) => { | ||
| 993 | 994 | assert.deepStrictEqual(clone, opts2); | |
| 994 | 995 | }) }; | |
| 995 | 996 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments