Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbhmr authored Aug 22, 2023
1 parent 1231bfc commit 121e1c7
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 207 deletions.
48 changes: 24 additions & 24 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
name: Publish npm
on:
release:
types: published
workflow_dispatch:
concurrency: ${{ github.workflow }}
jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache: npm
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm test
- run: npm publish --tag next
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
- if: '!github.event.release.prerelease'
run: npm dist-tag add "$(npm view . name)@$(npm view . version)" latest
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
name: Publish npm
on:
release:
types: published
workflow_dispatch:
concurrency: ${{ github.workflow }}
jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache: npm
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm test
- run: npm publish --tag next
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
- if: "!github.event.release.prerelease"
run: npm dist-tag add "$(npm view . name)@$(npm view . version)" latest
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
68 changes: 34 additions & 34 deletions .github/workflows/test-nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
name: Test
on:
push:
branches: "main"
paths-ignore:
- .gitignore
- LICENSE
- README.md
- .github/**
- "!.github/workflows/test-nodejs.yml"
pull_request:
paths:
- .gitignore
- LICENSE
- README.md
- .github/**
- "!.github/workflows/test-nodejs.yml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
strategy:
matrix:
node-version: ["16", "18", "20"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm test
name: Test
on:
push:
branches: "main"
paths-ignore:
- .gitignore
- LICENSE
- README.md
- .github/**
- "!.github/workflows/test-nodejs.yml"
pull_request:
paths:
- .gitignore
- LICENSE
- README.md
- .github/**
- "!.github/workflows/test-nodejs.yml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
strategy:
matrix:
node-version: ["16", "18", "20"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm test
140 changes: 71 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ is-what is really easy to use, and most functions work just like you'd expect.

```js
// import functions you want to use like so:
import { isString, isDate, isPlainObject } from 'is-what'
import { isString, isDate, isPlainObject } from "is-what";
```

1. First I'll go over the simple functions available. Only `isNumber` and `isDate` have special treatment.
Expand All @@ -42,59 +42,59 @@ import { isString, isDate, isPlainObject } from 'is-what'

```js
// basics
isBoolean(true) // true
isBoolean(false) // true
isUndefined(undefined) // true
isNull(null) // true
isBoolean(true); // true
isBoolean(false); // true
isUndefined(undefined); // true
isNull(null); // true

// strings
isString('') // true
isEmptyString('') // true
isFullString('') // false
isString(""); // true
isEmptyString(""); // true
isFullString(""); // false

// numbers
isNumber(0) // true
isNumber('0') // false
isNumber(NaN) // false *
isPositiveNumber(1) // true
isNegativeNumber(-1) // true
isNumber(0); // true
isNumber("0"); // false
isNumber(NaN); // false *
isPositiveNumber(1); // true
isNegativeNumber(-1); // true
// * see below for special NaN use cases!

// arrays
isArray([]) // true
isEmptyArray([]) // true
isFullArray([1]) // true
isArray([]); // true
isEmptyArray([]); // true
isFullArray([1]); // true

// objects
isPlainObject({}) // true *
isEmptyObject({}) // true
isFullObject({ a: 1 }) // true
isPlainObject({}); // true *
isEmptyObject({}); // true
isFullObject({ a: 1 }); // true
// * see below for special object (& class instance) use cases!

// functions
isFunction(function () {}) // true
isFunction(() => {}) // true
isFunction(function () {}); // true
isFunction(() => {}); // true

// dates
isDate(new Date()) // true
isDate(new Date('invalid date')) // false
isDate(new Date()); // true
isDate(new Date("invalid date")); // false

// maps & sets
isMap(new Map()) // true
isSet(new Set()) // true
isWeakMap(new WeakMap()) // true
isWeakSet(new WeakSet()) // true
isMap(new Map()); // true
isSet(new Set()); // true
isWeakMap(new WeakMap()); // true
isWeakSet(new WeakSet()); // true

// others
isRegExp(/\s/gi) // true
isSymbol(Symbol()) // true
isBlob(new Blob()) // true
isFile(new File([''], '', { type: 'text/html' })) // true
isError(new Error('')) // true
isPromise(new Promise((resolve) => {})) // true
isRegExp(/\s/gi); // true
isSymbol(Symbol()); // true
isBlob(new Blob()); // true
isFile(new File([""], "", { type: "text/html" })); // true
isError(new Error("")); // true
isPromise(new Promise((resolve) => {})); // true

// primitives
isPrimitive('') // true
isPrimitive(""); // true
// true for any of: boolean, null, undefined, number, string, symbol
```

Expand All @@ -104,36 +104,36 @@ isPrimitive('') // true

```js
// 1)
typeof NaN === 'number' // true
typeof NaN === "number"; // true
// 🤔 ("not a number" is a "number"...)

// 2)
isNaN('1') // false
isNaN("1"); // false
// 🤔 the string '1' is not-"not a number"... so it's a number??

// 3)
isNaN('one') // true
isNaN("one"); // true
// 🤔 'one' is NaN but `NaN === 'one'` is false...
```

With is-what the way we treat NaN makes a little bit more sense:

```js
import { isNumber, isNaNValue } from 'is-what'
import { isNumber, isNaNValue } from "is-what";

// 1)
isNumber(NaN) // false!
isNumber(NaN); // false!
// let's not treat NaN as a number

// 2)
isNaNValue('1') // false
isNaNValue("1"); // false
// if it's not NaN, it's not NaN!!

// 3)
isNaNValue('one') // false
isNaNValue("one"); // false
// if it's not NaN, it's not NaN!!

isNaNValue(NaN) // true
isNaNValue(NaN); // true
```

### isPlainObject vs isAnyObject
Expand All @@ -145,25 +145,27 @@ 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) {
this.speciality = somethingSpecial
this.speciality = somethingSpecial;
}
}
const specialObject = new SpecialObject('I am a special object! I am a class instance!!!')
const specialObject = new SpecialObject(
"I am a special object! I am a class instance!!!",
);

// check the plain object
isPlainObject(plainObject) // returns true
isAnyObject(plainObject) // returns true
getType(plainObject) // returns 'Object'
isPlainObject(plainObject); // returns true
isAnyObject(plainObject); // returns true
getType(plainObject); // returns 'Object'

// check the special object
isPlainObject(specialObject) // returns false !!!!!!!!!
isAnyObject(specialObject) // returns true
getType(specialObject) // returns 'Object'
isPlainObject(specialObject); // returns false !!!!!!!!!
isAnyObject(specialObject); // returns true
getType(specialObject); // returns 'Object'
```

> Please note that `isPlainObject` will only return `true` for normal plain JavaScript objects.
Expand All @@ -173,22 +175,22 @@ getType(specialObject) // returns 'Object'
You can check for specific types with `getType` and `isType`:

```js
import { getType, isType } from 'is-what'
import { getType, isType } from "is-what";

getType('') // returns 'String'
getType(""); // returns 'String'
// pass a Type as second param:
isType('', String) // returns true
isType("", String); // returns true
```

If you just want to make sure your object _inherits_ from a particular class or
`toStringTag` value, you can use `isInstanceOf()` like this:

```js
import { isInstanceOf } from 'is-what'
import { isInstanceOf } from "is-what";

isInstanceOf(new XMLHttpRequest(), 'EventTarget')
isInstanceOf(new XMLHttpRequest(), "EventTarget");
// returns true
isInstanceOf(globalThis, ReadableStream)
isInstanceOf(globalThis, ReadableStream);
// returns false
```

Expand All @@ -206,19 +208,19 @@ function isNumber(payload: any): payload is number {
function fn(payload: string | number): number {
if (isNumber(payload)) {
// ↑ TypeScript already knows payload is a number here!
return payload
return payload;
}
return 0
return 0;
}
```

`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(payload) && payload.id) return payload.id;
// if isPlainObject() would return `payload is object` then it would give an error at `payload.id`
```

Expand All @@ -229,9 +231,9 @@ If you want more control over what kind of interface/type is casted when checkin
To cast to a specific type while checking for `isAnyObject`, can use `isObjectLike<T>`:

```ts
import { isObjectLike } from 'is-what'
import { isObjectLike } from "is-what";

const payload = { name: 'Mesqueeb' } // current type: `{ name: string }`
const payload = { name: "Mesqueeb" }; // current type: `{ name: string }`

// Without casting:
if (isAnyObject(payload)) {
Expand All @@ -252,7 +254,7 @@ Please note: this library will not actually check the shape of the object, you n

```ts
function isObjectLike<T extends object>(payload: any): payload is T {
return isAnyObject(payload)
return isAnyObject(payload);
}
```

Expand All @@ -277,16 +279,16 @@ It's litterally just these functions:

```js
function getType(payload) {
return Object.prototype.toString.call(payload).slice(8, -1)
return Object.prototype.toString.call(payload).slice(8, -1);
}
function isUndefined(payload) {
return getType(payload) === 'Undefined'
return getType(payload) === "Undefined";
}
function isString(payload) {
return getType(payload) === 'String'
return getType(payload) === "String";
}
function isAnyObject(payload) {
return getType(payload) === 'Object'
return getType(payload) === "Object";
}
// etc...
```
Expand Down
Loading

0 comments on commit 121e1c7

Please sign in to comment.