Skip to content

Commit

Permalink
Merge branch 'production' of https://github.com/mesqueeb/is-what into…
Browse files Browse the repository at this point in the history
… production
  • Loading branch information
mesqueeb committed Mar 8, 2021
2 parents 0378888 + e94630d commit 6246ad4
Show file tree
Hide file tree
Showing 10 changed files with 7,892 additions and 18,694 deletions.
18 changes: 0 additions & 18 deletions .eslintrc.js

This file was deleted.

27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# is What? 🙉

Very simple & small JS type check functions. It's fully TypeScript supported!
Very simple & small JS type check functions. It's fully TypeScript supported!

```
npm i is-what
Expand All @@ -13,6 +13,7 @@ Or for deno available at: `"deno.land/x/is_what"`
I built is-what because the existing solutions were all too complex or too poorly built.

I was looking for:

- A simple way to check any kind of type (including non-primitives)
- Be able to check if an object is a plain object `{}` or a special object (like a class instance) ‼️
- Let TypeScript automatically know what type a value is when checking
Expand Down Expand Up @@ -85,11 +86,11 @@ Checking for a JavaScript object can be really difficult. In JavaScript you can

```js
// define a plain object
const plainObject = {hello: 'I am a good old object.'}
const plainObject = { hello: 'I am a good old object.' }

// define a special object
class SpecialObject {
constructor (somethingSpecial) {
constructor(somethingSpecial) {
this.speciality = somethingSpecial
}
}
Expand All @@ -113,13 +114,13 @@ getType(specialObject) // returns 'Object'
is-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users.

```ts
function isNumber (payload: any): payload is number {
function isNumber(payload: any): payload is number {
// return boolean
}
// As you can see above, all functions return a boolean for JavaScript, but pass the payload type to TypeScript.

// usage example:
function fn (payload: string | number): number {
function fn(payload: string | number): number {
if (isNumber(payload)) {
// ↑ TypeScript already knows payload is a number here!
return payload
Expand All @@ -131,8 +132,8 @@ function fn (payload: string | number): number {
`isPlainObject` and `isAnyObject` with TypeScript will declare the payload to be an object type with any props:

```ts
function isPlainObject (payload: any): payload is {[key: string]: any}
function isAnyObject (payload: any): payload is {[key: string]: any}
function isPlainObject(payload: any): payload is { [key: string]: any }
function isAnyObject(payload: any): payload is { [key: string]: any }
// The reason to return `{[key: string]: any}` is to be able to do
if (isPlainObject(payload) && payload.id) return payload.id
// if isPlainObject() would return `payload is object` then it would give an error at `payload.id`
Expand All @@ -145,15 +146,15 @@ If you want more control over which kind of objects are allowed you can use `isO
```ts
import { isObjectLike } from 'is-what'
// usage examples:
isObjectLike<{specificKey: string}>(payload)
isObjectLike<{ specificKey: string }>(payload)
isObjectLike<object>(payload)
// you can pass a specific type for TS to check on.
```

`isObjectLike<T>` works like this under the hood:

```ts
function isObjectLike<T extends object> (payload: any): payload is T {
function isObjectLike<T extends object>(payload: any): payload is T {
return isAnyObject(payload)
}
```
Expand All @@ -173,16 +174,16 @@ function isObjectLike<T extends object> (payload: any): payload is T {
It's litterally just these functions:

```js
function getType (payload) {
function getType(payload) {
return Object.prototype.toString.call(payload).slice(8, -1)
}
function isUndefined (payload) {
function isUndefined(payload) {
return getType(payload) === 'Undefined'
}
function isString (payload) {
function isString(payload) {
return getType(payload) === 'String'
}
function isAnyObject (payload) {
function isAnyObject(payload) {
return getType(payload) === 'Object'
}
// etc...
Expand Down
46 changes: 13 additions & 33 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-disable */

// npm install rollup-plugin-typescript2 typescript --save-dev
// npm i -D rollup rollup-plugin-typescript2 typescript
import typescript from 'rollup-plugin-typescript2'
// import { terser } from 'rollup-plugin-terser'
// import resolve from 'rollup-plugin-node-resolve'

// ------------------------------------------------------------------------------------------
// formats
Expand All @@ -20,41 +18,23 @@ import typescript from 'rollup-plugin-typescript2'
// ------------------------------------------------------------------------------------------
const pkg = require('./package.json')
const name = pkg.name
const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
const external = Object.keys(pkg.dependencies || [])
const plugins = [
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }),
]
const className = name.replace(/(^\w|-\w)/g, (c) => c.replace('-', '').toUpperCase())

// ------------------------------------------------------------------------------------------
// Builds
// ------------------------------------------------------------------------------------------
function defaults (config) {
// defaults
const defaults = {
plugins,
external,
}
// defaults.output
config.output = config.output.map(output => {
return Object.assign(
export default [
{
input: 'src/index.ts',
output: [
{
file: 'dist/index.js',
format: 'esm',
sourcemap: false,
name: className,
exports: 'named',
},
output
)
})
return Object.assign(defaults, config)
}

export default [
defaults({
input: 'src/index.ts',
output: [
{ file: 'dist/index.cjs.js', format: 'cjs' },
{ file: 'dist/index.esm.js', format: 'esm' },
],
}),
plugins: [
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }),
],
external: Object.keys(pkg.dependencies || []),
},
]
Loading

0 comments on commit 6246ad4

Please sign in to comment.