Skip to content

Commit

Permalink
Refactor exports
Browse files Browse the repository at this point in the history
- modularize code
  • Loading branch information
gilbarbara committed Oct 18, 2023
1 parent 84986cd commit f092069
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 178 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ is.string('value'); // true
You can also import any checker individually since 1.0

```ts
import { isString } from 'is-lite';
import { isString } from 'is-lite/exports';

isString('value'); // true
```
Expand Down
27 changes: 25 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,23 @@
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./exports": {
"import": "./dist/exports.mjs",
"require": "./dist/exports.js"
}
},
"files": [
"dist",
"src"
],
"typesVersions": {
"*": {
"exports": [
"dist/exports.d.ts"
]
}
},
"types": "dist/index.d.ts",
"sideEffects": false,
"devDependencies": {
Expand Down Expand Up @@ -72,17 +83,19 @@
"prepare": "husky install"
},
"tsup": {
"bundle": true,
"cjsInterop": true,
"dts": true,
"entry": [
"src/index.ts"
"src/index.ts",
"src/exports.ts"
],
"format": [
"cjs",
"esm"
],
"sourcemap": true,
"splitting": true
"splitting": false
},
"eslintConfig": {
"extends": [
Expand All @@ -100,6 +113,16 @@
"name": "esm",
"path": "./dist/index.mjs",
"limit": "1.5 kB"
},
{
"name": "exports-commonjs",
"path": "./dist/exports.js",
"limit": "2 kB"
},
{
"name": "exports-esm",
"path": "./dist/exports.mjs",
"limit": "2 kB"
}
]
}
39 changes: 39 additions & 0 deletions src/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-disable @typescript-eslint/ban-types */
import is from './index';

export const isArray = is.array;
export const isArrayOf = is.arrayOf;
export const isAsyncGeneratorFunction = is.asyncGeneratorFunction;
export const isAsyncFunction = is.asyncFunction;
export const isBigInt = is.bigint;
export const isBoolean = is.boolean;
export const isDate = is.date;
export const isDefined = is.defined;
export const isDomElement = is.domElement;
export const isEmpty = is.empty;
export const isError = is.error;
export const isFunction = is.function;
export const isGenerator = is.generator;
export const isGeneratorFunction = is.generatorFunction;
export const isInstanceOf = is.instanceOf;
export const isIterable = is.iterable;
export const isMap = is.map;
export const isNan = is.nan;
export const isNull = is.null;
export const isNullOrUndefined = is.nullOrUndefined;
export const isNumber = is.number;
export const isNumericString = is.numericString;
export const isObject = is.object;
export const isOneOf = is.oneOf;
export const isPlainFunction = is.plainFunction;
export const isPlainObject = is.plainObject;
export const isPrimitive = is.primitive;
export const isPromise = is.promise;
export const isPropertyOf = is.propertyOf;
export const isRegexp = is.regexp;
export const isSet = is.set;
export const isString = is.string;
export const isSymbol = is.symbol;
export const isUndefined = is.undefined;
export const isWeakMap = is.weakMap;
export const isWeakSet = is.weakSet;
64 changes: 64 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable @typescript-eslint/ban-types */
import type { ObjectTypes, Primitive, PrimitiveTypes } from './types';

export const objectTypes = [
'Array',
'ArrayBuffer',
'AsyncFunction',
'AsyncGenerator',
'AsyncGeneratorFunction',
'Date',
'Error',
'Function',
'Generator',
'GeneratorFunction',
'HTMLElement',
'Map',
'Object',
'Promise',
'RegExp',
'Set',
'WeakMap',
'WeakSet',
] as const;

export const primitiveTypes = [
'bigint',
'boolean',
'null',
'number',
'string',
'symbol',
'undefined',
] as const;

export function getObjectType(value: unknown): ObjectTypes | undefined {
const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);

if (/HTML\w+Element/.test(objectTypeName)) {
return 'HTMLElement';
}

if (isObjectType(objectTypeName)) {
return objectTypeName;
}

return undefined;
}

export function isObjectOfType<T>(type: string) {
return (value: unknown): value is T => getObjectType(value) === type;
}

export function isObjectType(name: unknown): name is ObjectTypes {
return objectTypes.includes(name as ObjectTypes);
}

export function isOfType<T extends Primitive | Function>(type: string) {
// eslint-disable-next-line valid-typeof
return (value: unknown): value is T => typeof value === type;
}

export function isPrimitiveType(name: unknown): name is PrimitiveTypes {
return primitiveTypes.includes(name as PrimitiveTypes);
}
Loading

0 comments on commit f092069

Please sign in to comment.