@bouko/ts 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import type { JsonMap, JsonValue } from "./types/generic";
2
- export * from "./types/generic";
3
- export * from "./types/work";
4
- export * from "./utils/checkers";
5
- export * from "./utils/formatters";
6
- export * from "./utils/parsers";
7
- export declare const safely: <T>(action: () => T) => T | undefined;
8
- export declare const safelyThru: <T>(arr: T[], action: (item: T) => void) => void;
9
- export declare const isJsonMap: (value: unknown) => value is JsonMap;
10
- export declare const isJsonValue: (value: unknown) => value is JsonValue;
1
+ import type { JsonMap, JsonValue } from "./types/generic";
2
+ export * from "./types/generic";
3
+ export * from "./types/work";
4
+ export * from "./utils/checkers";
5
+ export * from "./utils/formatters";
6
+ export * from "./utils/parsers";
7
+ export declare const safely: <T>(action: () => T) => T | undefined;
8
+ export declare const safelyThru: <T>(arr: T[], action: (item: T) => void) => void;
9
+ export declare const isJsonMap: (value: unknown) => value is JsonMap;
10
+ export declare const isJsonValue: (value: unknown) => value is JsonValue;
package/dist/index.js CHANGED
@@ -1,36 +1,36 @@
1
- export * from "./types/generic";
2
- export * from "./types/work";
3
- export * from "./utils/checkers";
4
- export * from "./utils/formatters";
5
- export * from "./utils/parsers";
6
- export const safely = (action) => {
7
- try {
8
- return action();
9
- }
10
- catch (err) {
11
- return undefined;
12
- }
13
- };
14
- export const safelyThru = (arr, action) => {
15
- try {
16
- arr.forEach(action);
17
- }
18
- catch {
19
- }
20
- };
21
- export const isJsonMap = (value) => {
22
- if (typeof value !== "object" ||
23
- value === null ||
24
- Array.isArray(value))
25
- return false;
26
- return Object.entries(value)
27
- .every(([_, v]) => isJsonValue(v) ||
28
- (Array.isArray(v) &&
29
- v.every(isJsonValue)));
30
- };
31
- export const isJsonValue = (value) => (typeof value === "string" ||
32
- typeof value === "number" ||
33
- typeof value === "boolean" ||
34
- value === null ||
35
- value === undefined ||
36
- isJsonMap(value));
1
+ export * from "./types/generic";
2
+ export * from "./types/work";
3
+ export * from "./utils/checkers";
4
+ export * from "./utils/formatters";
5
+ export * from "./utils/parsers";
6
+ export const safely = (action) => {
7
+ try {
8
+ return action();
9
+ }
10
+ catch (err) {
11
+ return undefined;
12
+ }
13
+ };
14
+ export const safelyThru = (arr, action) => {
15
+ try {
16
+ arr.forEach(action);
17
+ }
18
+ catch {
19
+ }
20
+ };
21
+ export const isJsonMap = (value) => {
22
+ if (typeof value !== "object" ||
23
+ value === null ||
24
+ Array.isArray(value))
25
+ return false;
26
+ return Object.entries(value)
27
+ .every(([_, v]) => isJsonValue(v) ||
28
+ (Array.isArray(v) &&
29
+ v.every(isJsonValue)));
30
+ };
31
+ export const isJsonValue = (value) => (typeof value === "string" ||
32
+ typeof value === "number" ||
33
+ typeof value === "boolean" ||
34
+ value === null ||
35
+ value === undefined ||
36
+ isJsonMap(value));
@@ -1,15 +1,15 @@
1
- /**
2
- * Type definitions for common data structures.
3
- *
4
- * - `StringMap`: Map of string keys to string values.
5
- * - `JsonMap`: Object of primitives, arrays, or nested maps.
6
- * - `JsonValue`: Union type representing a valid JSON value.
7
- *
8
- * They are intended to facilitate type-safe handling
9
- * of generic objects (ex. forms)
10
- **/
11
- export type StringMap = Record<string, string>;
12
- export type JsonMap = {
13
- [key: string]: JsonValue | JsonValue[];
14
- };
15
- export type JsonValue = string | string[] | number | number[] | boolean | boolean[] | null | null[] | undefined | undefined[] | JsonMap | JsonMap[];
1
+ /**
2
+ * Type definitions for common data structures.
3
+ *
4
+ * - `StringMap`: Map of string keys to string values.
5
+ * - `JsonMap`: Object of primitives, arrays, or nested maps.
6
+ * - `JsonValue`: Union type representing a valid JSON value.
7
+ *
8
+ * They are intended to facilitate type-safe handling
9
+ * of generic objects (ex. forms).
10
+ **/
11
+ export declare type StringMap = Record<string, string>;
12
+ export declare type JsonMap = {
13
+ [key: string]: JsonValue | JsonValue[];
14
+ };
15
+ export declare type JsonValue = string | string[] | number | number[] | boolean | boolean[] | null | null[] | undefined | undefined[] | JsonMap | JsonMap[];
@@ -1,12 +1,12 @@
1
- /**
2
- * Type definitions for common data structures.
3
- *
4
- * - `StringMap`: Map of string keys to string values.
5
- * - `JsonMap`: Object of primitives, arrays, or nested maps.
6
- * - `JsonValue`: Union type representing a valid JSON value.
7
- *
8
- * They are intended to facilitate type-safe handling
9
- * of generic objects (ex. forms)
10
- **/
11
- export {};
12
- // clean
1
+ /**
2
+ * Type definitions for common data structures.
3
+ *
4
+ * - `StringMap`: Map of string keys to string values.
5
+ * - `JsonMap`: Object of primitives, arrays, or nested maps.
6
+ * - `JsonValue`: Union type representing a valid JSON value.
7
+ *
8
+ * They are intended to facilitate type-safe handling
9
+ * of generic objects (ex. forms).
10
+ **/
11
+ export {};
12
+ // clean
@@ -1,3 +1,3 @@
1
- export type Job = {
2
- job_id: string;
3
- };
1
+ export declare type Job = {
2
+ job_id: string;
3
+ };
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,2 +1,2 @@
1
- export declare const isNum: (x: string) => boolean;
2
- export declare const isBool: (x: string) => boolean;
1
+ export declare const isNum: (x: string) => boolean;
2
+ export declare const isBool: (x: string) => boolean;
@@ -1,5 +1,5 @@
1
- export const isNum = (x) => !isNaN(Number(x));
2
- export const isBool = (x) => {
3
- const lower = x.trim().toLowerCase();
4
- return lower === "true" || lower === "false";
5
- };
1
+ export const isNum = (x) => !isNaN(Number(x));
2
+ export const isBool = (x) => {
3
+ const lower = x.trim().toLowerCase();
4
+ return lower === "true" || lower === "false";
5
+ };
@@ -0,0 +1,5 @@
1
+ export declare function getEnv(key: string): string;
2
+ export declare function getEnv(key: string, fallback: string): string;
3
+ export declare function getEnv(key: string, fallback: number): number;
4
+ export declare function getEnv(key: string, fallback: boolean): boolean;
5
+ export declare function getEnv<T>(key: string): T;
@@ -0,0 +1,23 @@
1
+ import { parseStr } from "./parsers";
2
+ /**
3
+ * Retrieves and parses an environment variable.
4
+ *
5
+ * @param key - The variable name
6
+ * @param fallback - Backup if no value is found (optional)
7
+ *
8
+ * @returns {JsonValue} The value parsed based on whichever
9
+ * primitive type the string matches.
10
+ *
11
+ * @throws {Error} If the variable is not found in the
12
+ * environment and there is no fallback set.
13
+ **/
14
+ export function getEnv(key, fallback) {
15
+ const raw = process.env[key];
16
+ if (!raw || raw === "") {
17
+ if (fallback !== undefined)
18
+ return fallback;
19
+ throw new Error(`ENV Missing: '${key}'`);
20
+ }
21
+ return parseStr(raw);
22
+ }
23
+ // clean
@@ -1 +1 @@
1
- export declare const shortenStr: (str: string) => string;
1
+ export declare const shortenStr: (str: string) => string;
@@ -1,5 +1,5 @@
1
- export const shortenStr = (str) => {
2
- if (str.length <= 6)
3
- return str;
4
- return `${str.slice(0, 3)}...${str.slice(-3)}`;
5
- };
1
+ export const shortenStr = (str) => {
2
+ if (str.length <= 6)
3
+ return str;
4
+ return `${str.slice(0, 3)}...${str.slice(-3)}`;
5
+ };
@@ -1,4 +1,11 @@
1
- import { JsonValue } from "../types/generic";
2
- export declare const parseStr: <T extends JsonValue>(input: string) => T;
3
- export declare const parseNum: (input: string) => number | undefined;
4
- export declare const parseBool: (input: string) => boolean;
1
+ import { JsonValue } from "../types/generic";
2
+ /**
3
+ * Parses a string value into it's appropriate
4
+ * type, or at least cleans up the string.
5
+ *
6
+ * @param {string} input - The text to parse.
7
+ *
8
+ * @returns {JsonValue} The value parsed based on whichever
9
+ * primitive type the string matches.
10
+ **/
11
+ export declare const parseStr: (input: string) => JsonValue;
@@ -1,26 +1,23 @@
1
- import { isNum, isBool } from "./checkers";
2
- export const parseStr = (input) => {
3
- if (isNum(input))
4
- return parseNum(input);
5
- else if (isBool(input))
6
- return parseBool(input);
7
- try {
8
- return JSON.parse(input);
9
- }
10
- catch { }
11
- return input.trim();
12
- };
13
- export const parseNum = (input) => {
14
- const num = Number(input.trim());
15
- if (!isNaN(num))
16
- return num;
17
- };
18
- export const parseBool = (input) => {
19
- const lower = input.trim().toLowerCase();
20
- if (lower === "true")
21
- return true;
22
- if (lower === "false")
23
- return false;
24
- else
25
- throw new Error(`Invalid boolean: ${input}`);
26
- };
1
+ /**
2
+ * Parses a string value into it's appropriate
3
+ * type, or at least cleans up the string.
4
+ *
5
+ * @param {string} input - The text to parse.
6
+ *
7
+ * @returns {JsonValue} The value parsed based on whichever
8
+ * primitive type the string matches.
9
+ **/
10
+ export const parseStr = (input) => {
11
+ const trimmed = input.trim();
12
+ if (/^-?\d+(\.\d+)?$/.test(trimmed) && !isNaN(Number(trimmed)))
13
+ return Number(trimmed);
14
+ if (/^(true|false)$/i.test(trimmed))
15
+ return trimmed.toLowerCase() === "true";
16
+ try {
17
+ return JSON.parse(trimmed);
18
+ }
19
+ catch {
20
+ return trimmed;
21
+ }
22
+ };
23
+ // clean
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@bouko/ts",
4
- "version": "0.2.0",
4
+ "version": "0.2.2",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "license": "MIT",