Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiny-decoders

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-decoders - npm Package Compare versions

Comparing version 3.1.0 to 4.0.0

21

CHANGELOG.md

@@ -0,1 +1,22 @@

### Version 4.0.0 (2019-09-29)
- Removed: The “stack trace,” showing you a little of each parent object and
array, in error messages is now gone. After using tiny-decoders for a while I
noticed this not being super useful. It’s nicer to look at the whole object in
a tool of choice, and just use the error message to understand _where_ the
error is, and what is wrong.
- Changed: `repr.short` is now called `repr.sensitive` because of the above
change.
- Removed: The `key` option of `repr`. It’s not needed since the “stack traces”
were removed.
- Changed: Object keys in the part showing you _where_ an error occurred are no
longer truncated.
- Changed: Literals, such as strings, are now allowed to be 100 characters long
before being truncated. Inside objects and arrays, the limit is 20 characters,
just like before. The idea is that printed values are at most 100–120
characters roughly. Now, strings and other literals can utilize more of that
space (rather than always being clipped already at 20 characters).
- Added: The `maxLength` and `recurseMaxLength` options of `repr` which control
the above change.
### Version 3.1.0 (2019-09-15)

@@ -2,0 +23,0 @@

5

dist/index.d.ts

@@ -99,9 +99,10 @@ // TODO: TypeScript Version: 3.0

options?: {
key?: string | number;
recurse?: boolean;
maxArrayChildren?: number;
maxObjectChildren?: number;
maxLength?: number;
recurseMaxLength?: number;
}
): string;
short: boolean;
sensitive: boolean;
};

71

dist/index.js

@@ -361,3 +361,3 @@ "use strict";

repr["short"] = false;
repr.sensitive = false;

@@ -367,3 +367,2 @@ function repr( // $FlowIgnore: Using `any` rather than `mixed` here to cut down on the bytes.

var _ref = _temp === void 0 ? {} : _temp,
key = _ref.key,
_ref$recurse = _ref.recurse,

@@ -374,3 +373,7 @@ recurse = _ref$recurse === void 0 ? true : _ref$recurse,

_ref$maxObjectChildre = _ref.maxObjectChildren,
maxObjectChildren = _ref$maxObjectChildre === void 0 ? 3 : _ref$maxObjectChildre;
maxObjectChildren = _ref$maxObjectChildre === void 0 ? 3 : _ref$maxObjectChildre,
_ref$maxLength = _ref.maxLength,
maxLength = _ref$maxLength === void 0 ? 100 : _ref$maxLength,
_ref$recurseMaxLength = _ref.recurseMaxLength,
recurseMaxLength = _ref$recurseMaxLength === void 0 ? 20 : _ref$recurseMaxLength;

@@ -382,11 +385,11 @@ var type = typeof value;

if (value == null || type === "number" || type === "boolean" || type === "symbol" || toStringType === "RegExp") {
return repr["short"] ? toStringType.toLowerCase() : truncate(String(value));
return repr.sensitive ? toStringType.toLowerCase() : truncate(String(value), maxLength);
}
if (type === "string") {
return repr["short"] ? type : printString(value);
return repr.sensitive ? type : truncate(JSON.stringify(value), maxLength);
}
if (type === "function") {
return "function " + printString(value.name);
return "function " + truncate(JSON.stringify(value.name), maxLength);
}

@@ -402,17 +405,12 @@

var lastIndex = _arr.length - 1;
var items = []; // Print values around the provided key, if any.
var items = [];
var end = Math.min(maxArrayChildren - 1, lastIndex);
var start = typeof key === "number" ? Math.max(0, Math.min(key, lastIndex - maxArrayChildren + 1)) : 0;
var end = Math.min(start + maxArrayChildren - 1, lastIndex);
if (start > 0) {
items.push("(" + start + " more)");
}
for (var _index4 = start; _index4 <= end; _index4++) {
for (var _index4 = 0; _index4 <= end; _index4++) {
var _item = _index4 in _arr ? repr(_arr[_index4], {
recurse: false
recurse: false,
maxLength: recurseMaxLength
}) : "<empty>";
items.push(_index4 === key ? "(index " + _index4 + ") " + _item : _item);
items.push(_item);
}

@@ -435,13 +433,10 @@

return name + "(" + keys.length + ")";
} // Make sure the provided key (if any) comes first, so that it is visible.
}
var numHidden = Math.max(0, keys.length - maxObjectChildren);
var newKeys = typeof key === "string" && keys.indexOf(key) >= 0 ? [key].concat(keys.filter(function (key2) {
return key2 !== key;
})) : keys;
var numHidden = Math.max(0, newKeys.length - maxObjectChildren);
var _items = newKeys.slice(0, maxObjectChildren).map(function (key2) {
return printString(key2) + ": " + repr(_obj[key2], {
recurse: false
var _items = keys.slice(0, maxObjectChildren).map(function (key2) {
return truncate(JSON.stringify(key2), recurseMaxLength) + ": " + repr(_obj[key2], {
recurse: false,
maxLength: recurseMaxLength
});

@@ -460,26 +455,14 @@ }).concat(numHidden > 0 ? "(" + numHidden + " more)" : []);

function printString(str) {
return truncate(JSON.stringify(str));
function truncate(str, maxLength) {
var half = Math.floor(maxLength / 2);
return str.length <= maxLength ? str : str.slice(0, half) + "\u2026" + str.slice(-half);
}
function truncate(str) {
// If the string is too long, show a bit at the start and a bit at the end and
// cut out the middle (replacing it with a separator). Explaining the magic
// numbers: 20 is the maximum length and: `20 = 10 + "…".length + 9`.
// `maxLength` and `separator` could be taken as parameters and the offset
// could be calculated from them, but I’ve hardcoded them to save some bytes.
return str.length <= 20 ? str : [str.slice(0, 10), "…", str.slice(-9)].join("");
}
var keyErrorPrefixRegex = /^(?:object|array)(?=\[)/;
function keyErrorMessage(key, value, message) {
var prefix = typeof key === "string" ? "object" : "array";
var at = typeof key === "string" ? printString(key) : String(key);
var missing = typeof key === "number" ? Array.isArray(value) && (key < 0 || key >= value.length) ? " (out of bounds)" : "" : value == null || typeof value !== "object" ?
var missing = typeof key === "number" ? Array.isArray(value) && (key < 0 || key >= value.length) ? "(out of bounds)" : "" : value == null || typeof value !== "object" ?
/* istanbul ignore next */
"" : Object.prototype.hasOwnProperty.call(value, key) ? "" : key in value ? " (prototype)" : " (missing)";
return [prefix + "[" + at + "]", keyErrorPrefixRegex.test(message) ? "" : ": ", message.replace(keyErrorPrefixRegex, ""), repr["short"] ? "" : "\nat " + at + missing + " in " + repr(value, {
key: key
})].join("");
"" : Object.prototype.hasOwnProperty.call(value, key) ? "" : key in value ? "(prototype)" : "(missing)";
return [(typeof key === "string" ? "object" : "array") + "[" + JSON.stringify(key) + "]", keyErrorPrefixRegex.test(message) ? "" : missing !== "" ? " " + missing + ": " : ": ", message.replace(keyErrorPrefixRegex, "")].join("");
}
{
"name": "tiny-decoders",
"version": "3.1.0",
"version": "4.0.0",
"license": "MIT",

@@ -37,5 +37,5 @@ "author": "Simon Lydell",

"devDependencies": {
"@babel/cli": "7.6.0",
"@babel/core": "7.6.0",
"@babel/preset-env": "7.6.0",
"@babel/cli": "7.6.2",
"@babel/core": "7.6.2",
"@babel/preset-env": "7.6.2",
"@babel/preset-flow": "7.0.0",

@@ -54,5 +54,5 @@ "babel-core": "7.0.0-bridge.0",

"eslint-plugin-jest": "22.17.0",
"eslint-plugin-prettier": "3.1.0",
"eslint-plugin-prettier": "3.1.1",
"eslint-plugin-simple-import-sort": "4.0.0",
"flow-bin": "0.107.0",
"flow-bin": "0.108.0",
"jest": "24.9.0",

@@ -59,0 +59,0 @@ "npm-run-all": "4.1.5",

@@ -43,3 +43,3 @@ # tiny-decoders [![Build Status][travis-badge]][travis-link] ![no dependencies][deps-tiny-decoders] [![minified size][min-tiny-decoders]][bundlephobia-tiny-decoders]

- [`repr`](#repr)
- [Short output (for sensitive data)](#short-output-for-sensitive-data)
- [Output for sensitive data](#output-for-sensitive-data)
- [Comparison with nvie/decoders](#comparison-with-nviedecoders)

@@ -103,3 +103,2 @@ - [Error messages](#error-messages)

TypeError: object["age"]: (optional) Expected a number, but got: "30"
at "age" in {"age": "30", "name": "John Doe", "active": true, (2 more)}
*/

@@ -916,6 +915,7 @@ ```

options?: {
key?: string | number;
recurse?: boolean;
maxArrayChildren?: number;
maxObjectChildren?: number;
maxLength?: number;
recurseMaxLength?: number;
}

@@ -930,8 +930,9 @@ ): string;

| name | type | default | description |
| ----------------- | -------------------------------------------------- | ----------- | ------------------------------------------------------------------------------------------- |
| key | <code>string &vert; number &vert; undefined</code> | `undefined` | An object key or array index to highlight when `repr`ing objects or arrays. |
| recurse | `boolean` | `true` | Whether to recursively call `repr` on array items and object values. It only recurses once. |
| maxArrayChildren | `number` | `5` | The number of array items to print (when `recurse` is `true`.) |
| maxObjectChildren | `number` | `3` | The number of object key-values to print (when `recurse` is `true`.) |
| name | type | default | description |
| ----------------- | --------- | ------- | ------------------------------------------------------------------------------------------------------------------- |
| recurse | `boolean` | `true` | Whether to recursively call `repr` on array items and object values. It only recurses once. |
| maxArrayChildren | `number` | `5` | The number of array items to print (when `recurse` is `true`.) |
| maxObjectChildren | `number` | `3` | The number of object key-values to print (when `recurse` is `true`.) |
| maxLength | `number` | `100` | The maximum length of literals, such as strings, before truncating them. |
| recurseMaxLength | `number` | `20` | Like `maxLength`, but when recursing. One typically wants shorter lengths here to avoid overly long error messages. |

@@ -961,3 +962,3 @@ Example:

#### Short output (for sensitive data)
#### Output for sensitive data

@@ -968,8 +969,7 @@ By default, the tiny-decoder’s error messages try to be helpful by showing you

addresses, passwords or social security numbers, you might not want that data to
potentially appear in error logs. Another use case is if you simply prefer a
shorter, oneline message.
potentially appear in error logs.
By setting `repr.short = true` you will get shorter error messages, containing
only _where_ the error happened and the actual and expected types, but not
showing any actual values.
By setting `repr.sensitive = true` you will get error messages containing only
_where_ the error happened and the actual and expected types, but not showing
any actual values.

@@ -980,7 +980,5 @@ Standard:

object["details"]["ssn"]: Expected a string, but got: 123456789
at "ssn" in {"ssn": 123456789, "email": "john.doe@…mple.com"}
at "details" in {"details": Object(2), "name": "John Doe"}
```
With `repr.short = true`:
With `repr.sensitive = true`:

@@ -992,8 +990,8 @@ ```

All decoders use `repr` internally when making their error messages, so setting
`repr.short` affect them too. This is admittedly not the most beautiful API, but
it is tiny.
`repr.sensitive` affect them too. This is admittedly not the most beautiful API,
but it is tiny.
If you need _both_ standard _and_ short output in the same application –
remember that `repr.short = true` globally affects everything. You’ll need to
flip `repr.short` back and forth as needed.
If you need _both_ standard _and_ sensitive output in the same application –
remember that `repr.sensitive = true` globally affects everything. You’ll need
to flip `repr.sensitive` back and forth as needed.

@@ -1065,12 +1063,8 @@ ## Comparison with nvie/decoders

The errors of tiny-decoders are shorter and a little bit more cryptic. As
opposed to [nvie/decoders], it stops at the _first_ error in a record (instead
of showing them all). First, the missing “id” field:
The errors of tiny-decoders are way shorter. As opposed to [nvie/decoders], it
stops at the _first_ error in a record (instead of showing them all). First, the
missing “id” field:
```
TypeError: array[1]["accessories"][0]["id"]: Expected a string, but got: undefined
at "id" (missing) in {"name": "Keycap Puller", "image": "data:imag…AkQBADs=", "discount": "5%"}
at 0 in [(index 0) Object(3), Object(4)]
at "accessories" in {"accessories": Array(2), "id": "382973", "name": "Ergonomic Keyboard", (2 more)}
at 1 in [Object(5), (index 1) Object(5), Object(5)]
```

@@ -1082,25 +1076,10 @@

TypeError: array[1]["accessories"][0]["discount"]: (optional) Expected a number, but got: "5%"
at "discount" in {"discount": "5%", "id": "489382", "name": "Keycap Puller", (1 more)}
at 0 in [(index 0) Object(4), Object(4)]
at "accessories" in {"accessories": Array(2), "id": "382973", "name": "Ergonomic Keyboard", (2 more)}
at 1 in [Object(5), (index 1) Object(5), Object(5)]
```
If you read the “stack trace” of tiny-decoders from bottom to top, it’s a bit
like expanding objects and arrays in the browser devtools (but in your head):
tiny-decoders used to also print a “stack trace,” showing you a little of each
parent object and array. After using tiny-decoders for a while I noticed this
not being super useful. It’s nicer to look at the whole object in a tool of
choice, and just use the error message to understand _where_ the error is, and
what is wrong.
```
[Object(5), (index 1) Object(5), Object(5)]
|
v
{"accessories": Array(2), "id": "382973", "name": "Ergonomic Keyboard", (2 more)}
|
v
[(index 0) Object(4), Object(4)]
|
v
{"discount": "5%", "id": "489382", "name": "Keycap Puller", (1 more)}
^^^^
```
## Development

@@ -1107,0 +1086,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc