Skip to content

Commit

Permalink
Initial type-guarden release
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronccasanova committed Nov 13, 2022
1 parent f9a2667 commit ba4b49f
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-lamps-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'type-guarden': patch
---

Initial release
10 changes: 10 additions & 0 deletions packages/type-guarden/.babelrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@aacc/babel-preset",
{
"typescript": true
}
]
]
}
12 changes: 12 additions & 0 deletions packages/type-guarden/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @type {import('eslint').Linter.Config}
*/
module.exports = {
root: true,
extends: ['@aacc/eslint-config/typescript'],
parserOptions: {
tsconfigRootDir: __dirname,
project: 'tsconfig.eslint.json',
},
ignorePatterns: ['node_modules', 'dist'],
}
30 changes: 30 additions & 0 deletions packages/type-guarden/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# type-guarden

Collection of type guards for TypeScript.

## Installation

```sh
npm install type-guarden
```

## Usage

```ts
import { isKeyOf, isValueOf } from 'type-guarden'

const obj = {
foo: 'bar',
baz: 42,
}

const arr = ['foo', 'bar', 'baz']

if (isKeyOf(obj, key)) {
// `key` is 'foo' | 'baz'
}

if (isValueOf(arr, value)) {
// `value` is 'foo' | 'bar' | 'baz'
}
```
62 changes: 62 additions & 0 deletions packages/type-guarden/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "type-guarden",
"version": "0.0.0",
"description": "Collection of type guards for TypeScript",
"author": "Aaron Casanova <aaronccasanova@gmail.com>",
"license": "MIT",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
"browser": "dist/browser/index.js",
"types": "dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js"
}
},
"scripts": {
"dev": "npm-run-all --parallel 'build:* -- --watch'",
"build": "npm-run-all --parallel build:*",
"build:js": "rollup -c",
"build:types": "tsc --emitDeclarationOnly",
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"lint": "TIMING=1 eslint . --ext .js,.ts --cache",
"prepublishOnly": "npm run build"
},
"files": [
"dist"
],
"peerDependencies": {},
"dependencies": {
"the-answer": "^1.0.0"
},
"devDependencies": {
"@aacc/babel-preset": "*",
"@aacc/browserslist-config": "*",
"@aacc/eslint-config": "*",
"@aacc/tsconfigs": "*",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^21.1.0",
"@rollup/plugin-node-resolve": "^13.2.1",
"rollup": "^2.70.2",
"typescript": "^4.7.3"
},
"browserslist": [
"extends @aacc/browserslist-config"
],
"publishConfig": {
"access": "public",
"@aacc:registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aaronccasanova/aacc.git",
"directory": "packages/type-guarden"
},
"bugs": {
"url": "https://github.com/aaronccasanova/aacc/issues"
},
"homepage": "https://github.com/aaronccasanova/aacc/blob/main/packages/type-guarden/README.md"
}
57 changes: 57 additions & 0 deletions packages/type-guarden/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import path from 'path'

import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import babel from '@rollup/plugin-babel'

import pkg from './package.json'

const name = 'typeGuarden'

const extensions = ['.js', '.jsx', '.ts', '.tsx']

/**
* @type {import('rollup').RollupOptions}
*/
export default {
input: 'src/index.ts',
output: [
{
format: /** @type {const} */ ('cjs'),
entryFileNames: '[name][assetExtname].js',
dir: path.dirname(pkg.main),
preserveModules: true,
},
{
format: /** @type {const} */ ('es'),
entryFileNames: '[name][assetExtname].mjs',
dir: path.dirname(pkg.module),
preserveModules: true,
},
{
format: /** @type {const} */ ('iife'),
file: pkg.browser,
name,

// https://rollupjs.org/guide/en/#outputglobals
// globals: {},
},
],
plugins: [
// Allows node_modules resolution
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
nodeResolve({ extensions }),
// Allow bundling cjs modules. Rollup doesn't understand cjs
commonjs(),
// Compile TypeScript/JavaScript files
babel({
extensions,
babelHelpers: 'bundled',
include: ['src/**/*'],
}),
],
external: [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.peerDependencies ?? {}),
],
}
13 changes: 13 additions & 0 deletions packages/type-guarden/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function isKeyOf<T extends { [key: string]: any }>(
obj: T,
key?: PropertyKey | undefined,
): key is keyof T {
return Object.keys(obj).includes(key as string)
}

export function isValueOf<T extends readonly any[]>(
arr: T,
value?: any | undefined,
): value is T[number] {
return arr.includes(value)
}
14 changes: 14 additions & 0 deletions packages/type-guarden/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// https://typescript-eslint.io/docs/linting/monorepo#one-root-tsconfigjson
"extends": "./tsconfig.json",
"compilerOptions": {
// Ensures this config is not used for a build
"noEmit": true
},
"include": [
// Paths to lint
"src",
".eslintrc.js",
"rollup.config.js"
]
}
9 changes: 9 additions & 0 deletions packages/type-guarden/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@aacc/tsconfigs/node-library.json",
"compilerOptions": {
"outDir": "dist",
"declarationDir": "dist/types"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}

3 comments on commit ba4b49f

@vercel
Copy link

@vercel vercel bot commented on ba4b49f Nov 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on ba4b49f Nov 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

aacc-next-ts – ./recipes/next-ts

aacc-next-ts.vercel.app
aacc-next-ts-git-main-aaronccasanova-gmailcom.vercel.app
aacc-next-ts-aaronccasanova-gmailcom.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ba4b49f Nov 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

aacc-next-ts-styled-comps – ./recipes/next-ts-styled-comps

aacc-next-ts-styled-comps.vercel.app
aacc-next-ts-styled-comps-aaronccasanova-gmailcom.vercel.app
aacc-next-ts-styled-comps-git-main-aaronccasanova-gmailcom.vercel.app

Please sign in to comment.