append-type
Advanced tools
Comparing version 0.0.0 to 1.0.0
{ | ||
"name": "append-type", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Stringify the value with appending its type: 10 → '10 (number)'", | ||
@@ -5,0 +5,0 @@ "repository": "shinnn/append-type", |
@@ -1,20 +0,16 @@ | ||
# sub-index | ||
# append-type | ||
[![NPM version](https://img.shields.io/npm/v/sub-index.svg)](https://www.npmjs.com/package/sub-index) | ||
[![Bower version](https://img.shields.io/bower/v/sub-index.svg)](https://github.com/shinnn/sub-index/releases) | ||
[![Build Status](https://travis-ci.org/shinnn/sub-index.svg?branch=master)](https://travis-ci.org/shinnn/sub-index) | ||
[![Coverage Status](https://img.shields.io/coveralls/shinnn/sub-index.svg)](https://coveralls.io/r/shinnn/sub-index) | ||
[![devDependencies Status](https://david-dm.org/shinnn/sub-index/dev-status.svg)](https://david-dm.org/shinnn/sub-index?type=dev) | ||
[![NPM version](https://img.shields.io/npm/v/append-type.svg)](https://www.npmjs.com/package/append-type) | ||
[![Bower version](https://img.shields.io/bower/v/append-type.svg)](https://github.com/shinnn/append-type/releases) | ||
[![Build Status](https://travis-ci.org/shinnn/append-type.svg?branch=master)](https://travis-ci.org/shinnn/append-type) | ||
[![Coverage Status](https://img.shields.io/coveralls/shinnn/append-type.svg)](https://coveralls.io/r/shinnn/append-type) | ||
[![devDependencies Status](https://david-dm.org/shinnn/append-type/dev-status.svg)](https://david-dm.org/shinnn/append-type?type=dev) | ||
Find an array index of the substitute value that should be chosen when a given index is deleted | ||
Stringify the value with appending its [type](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof): `10` → ``'10 (number)'`` | ||
```javascript | ||
import subIndex from 'sub-index'; | ||
import appendType from 'append-type'; | ||
const array = new Array(3); | ||
subIndex(array, 1); //=> 0 | ||
subIndex(array, 2); //=> 1 | ||
subIndex(array, 0); //=> 1 | ||
appendType('123'); //=> 'foo (string)' | ||
appendType(123); //=> '123 (number)' | ||
``` | ||
@@ -27,3 +23,3 @@ | ||
``` | ||
npm install sub-index | ||
npm install append-type | ||
``` | ||
@@ -34,3 +30,3 @@ | ||
``` | ||
bower install sub-index | ||
bower install append-type | ||
``` | ||
@@ -40,47 +36,36 @@ | ||
### subIndex(*array*, *index*) | ||
### appendType(*value*) | ||
*array*: `Array` (non-empty array) | ||
*index*: `Number` (index of the array assumed to be deleted) | ||
Return: `Number` or `null` | ||
*value*: any type | ||
Return: `String` | ||
Essentially, it returns the predecessor number of a given `index`. | ||
Essentially, it returns `String(value) + ' (' + typeof value + ')'`. | ||
```javascript | ||
appendType(() => {}); //=> '() => {} (function)' | ||
``` | ||
index: 0 1 2 | ||
value: A B C | ||
deleted: ^ | ||
substitute: ^ | ||
``` | ||
When it takes `null` / `undefined`, it returns `'null'` / `'undefined'`. | ||
```javascript | ||
subIndex(['A', 'B', 'C'], 1); //=> 0 | ||
appendType(null); //=> 'null' | ||
appendType(undefined); //=> 'undefined' | ||
``` | ||
If `index` is `0`, it returns the successor number `1` because the first element has no predecessor elements. | ||
## Example | ||
``` | ||
index: 0 1 2 | ||
value: A B C | ||
deleted: ^ | ||
substitute: ^ | ||
``` | ||
This module is useful for making `TypeError` error messages. | ||
```javascript | ||
subIndex(['A', 'B', 'C'], 0); //=> 1 | ||
``` | ||
function reverse(v) { | ||
if (typeof v !== 'boolean') { | ||
throw new TypeError(`Expected a Boolean value, but got ${appendType(v)}.`); | ||
} | ||
If the array includes only a single value, it returns `null` because no value exists after deletion. | ||
return !v; | ||
}; | ||
reverse(1); //=> TypeError: Expected a Boolean value, but got 1 (number). | ||
``` | ||
index: 0 | ||
value: A | ||
deleted: ^ | ||
substitute: (none) | ||
``` | ||
```javascript | ||
subIndex(['A'], 0); //=> null | ||
``` | ||
## License | ||
@@ -87,0 +72,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
4475
73