ts-transformers
A TypeScript transformer that allows you to extract all values of constant enumurations declarations.
How to use this package
Installation
Install the module as a dev dependency.
npm i -D @vendredix/ts-transformers
How to use enumValues
import {enumValues} from "@vendredix/ts-transformers";
declare const enum Permissions {
READ = 1,
WRITE = 2,
READ_WRITE = READ | WRITE,
}
declare const enum Types {
TEXT = "text",
INTEGER = "integer",
}
const permissions = enumValues<Permissions>(); // [1, 2, 3];
const types = enumValues<Types>(); // ["text", "integer"];
How to use type guard functions
import {
isNull, isUndefined, isNullOrUndefined,
isBoolean, isNumber, isString, isFunction,
isPrimitive,
isArray, isObject, isObjectOrArray,
} from "@vendredix/ts-transformers";
let value: any;
if (isNullOrUndefined(value)) {
// (value === null || value === undefined)
// value is null | undefined
}
if (isFunction(value)) {
// (typeof value === "function")
// value is a function
}
if (isPrimitive(value)) {
// (typeof value === "number" || typeof value === "boolean" || typeof value === "string")
// value is number | boolean | string;
}
if (isObject(value)) {
// (typeof value === "object" && value !== null && !Array.isArray(value))
// value is an object and not null
}
How to use this transformer
Using @vendredix/ts-transformer's compiler interface. See tests/tsconfig.json for an example.
Update your tsconfig.json
as follows and compile with tstc <tsconfig.json>
or use compileByConfig
of compiler.ts.
{
"compilerOptions": {},
"vendredix": {
"ts-transformers": {
"plugins": [
{ "transform": "@vendredix/ts-transformers/transformer.mjs", "kind": ["before"] }
]
}
}
}
plugin entries described in PluginConfig:
export interface CompilerConfig {
sourceMapSupport?: boolean;
plugins?: PluginConfig[];
}
export declare const enum PluginType {
ProgramBuilder = "builder",
Program = "program"
}
export declare const enum TransformerKind {
Before = "before",
After = "after",
AfterDeclaration = "afterDeclaration"
}
export interface PluginConfig {
transform: string;
import?: string; // whenever it should be a specific exported function
kind?: TransformerKind[]; // default ["after"]
type?: PluginType; // default "program"
[options: string]: unknown;
}
Or using ttypescript. See their repository for more information.
{
"compilerOptions": {
"plugins": [
{ "transform": "@vendredix/ts-transformers/transformer.mjs", "type": "program" }
]
}
}