forked from orbit-oss/is
Readme tweaks
This commit is contained in:
parent
c66885b781
commit
79144f9542
2 changed files with 45 additions and 29 deletions
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "@sindresorhus/is",
|
||||
"version": "0.15.0",
|
||||
"description": "Type check values: `is.string('🦄') //=> true`",
|
||||
"description": "Type check values",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/is",
|
||||
"author": {
|
||||
|
|
@ -41,7 +41,10 @@
|
|||
"kind",
|
||||
"primitive",
|
||||
"verify",
|
||||
"compare"
|
||||
"compare",
|
||||
"typescript",
|
||||
"typeguards",
|
||||
"types"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@sindresorhus/tsconfig": "^0.2.0",
|
||||
|
|
|
|||
67
readme.md
67
readme.md
|
|
@ -1,10 +1,20 @@
|
|||
# is [](https://travis-ci.org/sindresorhus/is)
|
||||
|
||||
> Type check values: `is.string('🦄') //=> true`
|
||||
> Type check values
|
||||
|
||||
For example, `is.string('🦄') //=> true`
|
||||
|
||||
<img src="header.gif" width="182" align="right">
|
||||
|
||||
|
||||
## Highlights
|
||||
|
||||
- Written in TypeScript
|
||||
- [Extensive use of type guards](#type-guards)
|
||||
- Actively maintained
|
||||
- 2 million weekly downloads
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
|
|
@ -27,32 +37,6 @@ is.number(6);
|
|||
//=> true
|
||||
```
|
||||
|
||||
When using `is` together with TypeScript, [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) are being used to infer the correct type inside if-else statements.
|
||||
|
||||
```ts
|
||||
import is from '@sindresorhus/is';
|
||||
|
||||
const padLeft = (value: string, padding: string | number) => {
|
||||
if (is.number(padding)) {
|
||||
// `padding` is typed as `number`
|
||||
return Array(padding + 1).join(' ') + value;
|
||||
}
|
||||
|
||||
if (is.string(padding)) {
|
||||
// `padding` is typed as `string`
|
||||
return padding + value;
|
||||
}
|
||||
|
||||
throw new TypeError(`Expected 'padding' to be of type 'string' or 'number', got '${is(padding)}'.`);
|
||||
}
|
||||
|
||||
padLeft('🦄', 3);
|
||||
//=> ' 🦄'
|
||||
|
||||
padLeft('🦄', '🌈');
|
||||
//=> '🌈🦄'
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
|
|
@ -407,6 +391,35 @@ is.all(is.string, '🦄', [], 'unicorns');
|
|||
```
|
||||
|
||||
|
||||
## Type guards
|
||||
|
||||
When using `is` together with TypeScript, [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) are being used extensively to infer the correct type inside if-else statements.
|
||||
|
||||
```ts
|
||||
import is from '@sindresorhus/is';
|
||||
|
||||
const padLeft = (value: string, padding: string | number) => {
|
||||
if (is.number(padding)) {
|
||||
// `padding` is typed as `number`
|
||||
return Array(padding + 1).join(' ') + value;
|
||||
}
|
||||
|
||||
if (is.string(padding)) {
|
||||
// `padding` is typed as `string`
|
||||
return padding + value;
|
||||
}
|
||||
|
||||
throw new TypeError(`Expected 'padding' to be of type 'string' or 'number', got '${is(padding)}'.`);
|
||||
}
|
||||
|
||||
padLeft('🦄', 3);
|
||||
//=> ' 🦄'
|
||||
|
||||
padLeft('🦄', '🌈');
|
||||
//=> '🌈🦄'
|
||||
```
|
||||
|
||||
|
||||
## FAQ
|
||||
|
||||
### Why yet another type checking module?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue