typeshi 1.2.0 → 1.3.0

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.
@@ -2,7 +2,7 @@
2
2
  * @file src/config/setupLog.ts
3
3
  * @reference https://tslog.js.org/#/?id=pretty-templates-and-styles-color-settings
4
4
  */
5
- import { Logger, ILogObj } from 'tslog';
5
+ import { Logger, ISettingsParam, ILogObj, IPrettyLogStyles } from "tslog";
6
6
  /**
7
7
  * `TAB = INDENT_LOG_LINE = '\n\t• '` = newLine + tab + bullet + space
8
8
  * - log.debug(s1, INDENT_LOG_LINE + s2, INDENT_LOG_LINE + s3,...)
@@ -12,6 +12,30 @@ export declare const INDENT_LOG_LINE: string;
12
12
  * `NL = NEW_LINE = '\n > '` = newLine + space + > + space
13
13
  * */
14
14
  export declare const NEW_LINE: string;
15
+ export declare const logTemplates: {
16
+ /**= {@link timestampTemplate} + {@link logNameTemplate} + {@link logLevelTemplate} + {@link fileInfoTemplate} + `\n\t{{logObjMeta}}` */
17
+ DEFAULT_LOG_TEMPLATE: string;
18
+ /** `"{{errorName}}: {{errorMessage}}{INDENT_LOG_LINE}{{errorStack}}"` */
19
+ DEFAULT_ERROR_TEMPLATE: string;
20
+ /**`"${fileInfoTemplate}:{{method}} {{stack}}"` */
21
+ errorStackTemplate: string;
22
+ /** `"{{yyyy}}-{{mm}}-{{dd}}"` */
23
+ dateTemplate: string;
24
+ /** `"{{hh}}:{{MM}}:{{ss}}"` */
25
+ timeTemplate: string;
26
+ /** `"{{hh}}:{{MM}}:{{ss}}.{{ms}}"` */
27
+ msTimeTemplate: string;
28
+ /** `"(${dateTemplate} ${timeTemplate})"` */
29
+ timestampTemplate: string;
30
+ /** `"[{{name}}]"` */
31
+ logNameTemplate: string;
32
+ /** `"[{{logLevelName}}]"` */
33
+ logLevelTemplate: string;
34
+ /** `"{{filePathWithLine}}"` */
35
+ fileInfoTemplate: string;
36
+ };
37
+ export declare const PRETTY_LOG_STYLES: IPrettyLogStyles;
38
+ export declare const MAIN_LOGGER_SETTINGS: ISettingsParam<ILogObj>;
15
39
  /**
16
40
  * `type: "pretty"`
17
41
  * @example
@@ -26,5 +50,9 @@ export declare const NEW_LINE: string;
26
50
  *
27
51
  * */
28
52
  export declare const typeshiLogger: Logger<ILogObj>;
53
+ export declare const SIMPLE_LOG_TEMPLATE = " > ";
54
+ /** `type: "pretty"`, `template` = `" > {{logObjMeta}}"` */
55
+ export declare const SIMPLE_LOGGER_SETTINGS: ISettingsParam<ILogObj>;
29
56
  export declare const typeshiSimpleLogger: Logger<ILogObj>;
57
+ export declare const HIDDEN_LOGGER_SETTINGS: ISettingsParam<ILogObj>;
30
58
  export declare const typeshiHiddenLogger: Logger<ILogObj>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.typeshiHiddenLogger = exports.typeshiSimpleLogger = exports.typeshiLogger = exports.NEW_LINE = exports.INDENT_LOG_LINE = void 0;
3
+ exports.typeshiHiddenLogger = exports.HIDDEN_LOGGER_SETTINGS = exports.typeshiSimpleLogger = exports.SIMPLE_LOGGER_SETTINGS = exports.SIMPLE_LOG_TEMPLATE = exports.typeshiLogger = exports.MAIN_LOGGER_SETTINGS = exports.PRETTY_LOG_STYLES = exports.logTemplates = exports.NEW_LINE = exports.INDENT_LOG_LINE = void 0;
4
4
  /**
5
5
  * @file src/config/setupLog.ts
6
6
  * @reference https://tslog.js.org/#/?id=pretty-templates-and-styles-color-settings
@@ -17,6 +17,8 @@ exports.INDENT_LOG_LINE = '\n\t• ';
17
17
  exports.NEW_LINE = '\n > ';
18
18
  const dateTemplate = "{{yyyy}}-{{mm}}-{{dd}}";
19
19
  const timeTemplate = "{{hh}}:{{MM}}:{{ss}}"; //.{{ms}}";
20
+ /** `"{{hh}}:{{MM}}:{{ss}}.{{ms}}"` */
21
+ const msTimeTemplate = `${timeTemplate}.{{ms}}`;
20
22
  const timestampTemplate = `(${dateTemplate} ${timeTemplate})`;
21
23
  /**not included for now */
22
24
  const logNameTemplate = "[{{name}}]"; //"[{{nameWithDelimiterPrefix}}{{name}}{{nameWithDelimiterSuffix}}]";
@@ -29,11 +31,10 @@ const fileInfoTemplate = "{{filePathWithLine}}";
29
31
  * use as value for {@link ISettingsParam.prettyLogTemplate}
30
32
  * = {@link timestampTemplate} + {@link logNameTemplate} + {@link logLevelTemplate} + {@link fileInfoTemplate} + `\n\t{{logObjMeta}}`
31
33
  * - {@link timestampTemplate} = `({{yyyy}}-{{mm}}-{{dd}} {{hh}}:{{MM}}:{{ss}}.{{ms}})`
32
- * - {@link logNameTemplate} = `"[{{name}}]"`
33
34
  * - {@link logLevelTemplate} = `{{logLevelName}}:`
34
35
  * - {@link fileInfoTemplate} = `{{fileName}}:{{fileLine}}`
35
36
  * */
36
- const LOG_TEMPLATE = [
37
+ const DEFAULT_LOG_TEMPLATE = [
37
38
  logLevelTemplate,
38
39
  timestampTemplate,
39
40
  // logNameTemplate,
@@ -47,13 +48,35 @@ const errorInfoTemplate = [
47
48
  * use as value for {@link ISettingsParam.prettyErrorTemplate}
48
49
  * @description template string for error message.
49
50
  * */
50
- const ERROR_TEMPLATE = `${errorInfoTemplate}`; //`${timestampTemplate} ${logNameTemplate} ${logLevelTemplate} ${fileInfoTemplate}\n${errorInfoTemplate}`;
51
+ const DEFAULT_ERROR_TEMPLATE = `${errorInfoTemplate}`; //`${timestampTemplate} ${logNameTemplate} ${logLevelTemplate} ${fileInfoTemplate}\n${errorInfoTemplate}`;
51
52
  /**
52
53
  * use as value for {@link ISettingsParam.prettyErrorStackTemplate}.
53
54
  * @description template string for error stack trace lines.
54
55
  * */
55
- const ERROR_STACK_TEMPLATE = `${fileInfoTemplate}:{{method}} {{stack}}`;
56
- const PRETTY_LOG_STYLES = {
56
+ const errorStackTemplate = `${fileInfoTemplate}:{{method}} {{stack}}`;
57
+ exports.logTemplates = {
58
+ /**= {@link timestampTemplate} + {@link logNameTemplate} + {@link logLevelTemplate} + {@link fileInfoTemplate} + `\n\t{{logObjMeta}}` */
59
+ DEFAULT_LOG_TEMPLATE,
60
+ /** `"{{errorName}}: {{errorMessage}}{INDENT_LOG_LINE}{{errorStack}}"` */
61
+ DEFAULT_ERROR_TEMPLATE,
62
+ /**`"${fileInfoTemplate}:{{method}} {{stack}}"` */
63
+ errorStackTemplate,
64
+ /** `"{{yyyy}}-{{mm}}-{{dd}}"` */
65
+ dateTemplate,
66
+ /** `"{{hh}}:{{MM}}:{{ss}}"` */
67
+ timeTemplate,
68
+ /** `"{{hh}}:{{MM}}:{{ss}}.{{ms}}"` */
69
+ msTimeTemplate,
70
+ /** `"(${dateTemplate} ${timeTemplate})"` */
71
+ timestampTemplate,
72
+ /** `"[{{name}}]"` */
73
+ logNameTemplate,
74
+ /** `"[{{logLevelName}}]"` */
75
+ logLevelTemplate,
76
+ /** `"{{filePathWithLine}}"` */
77
+ fileInfoTemplate
78
+ };
79
+ exports.PRETTY_LOG_STYLES = {
57
80
  yyyy: "green",
58
81
  mm: "green",
59
82
  dd: "green",
@@ -82,16 +105,16 @@ const PRETTY_LOG_STYLES = {
82
105
  errorName: ["red", "bold"],
83
106
  errorMessage: "redBright",
84
107
  };
85
- const MAIN_LOGGER_SETTINGS = {
108
+ exports.MAIN_LOGGER_SETTINGS = {
86
109
  type: "pretty",
87
110
  name: "typeshi_main",
88
111
  minLevel: 0,
89
- prettyLogTemplate: LOG_TEMPLATE,
90
- prettyErrorTemplate: ERROR_TEMPLATE,
91
- prettyErrorStackTemplate: ERROR_STACK_TEMPLATE,
112
+ prettyLogTemplate: DEFAULT_LOG_TEMPLATE,
113
+ prettyErrorTemplate: DEFAULT_ERROR_TEMPLATE,
114
+ prettyErrorStackTemplate: errorStackTemplate,
92
115
  stylePrettyLogs: true,
93
116
  prettyLogTimeZone: "local",
94
- prettyLogStyles: PRETTY_LOG_STYLES,
117
+ prettyLogStyles: exports.PRETTY_LOG_STYLES,
95
118
  };
96
119
  /**
97
120
  * `type: "pretty"`
@@ -106,30 +129,30 @@ const MAIN_LOGGER_SETTINGS = {
106
129
  * });
107
130
  *
108
131
  * */
109
- exports.typeshiLogger = new tslog_1.Logger(MAIN_LOGGER_SETTINGS);
110
- const SIMPLE_LOG_TEMPLATE = ` > `;
132
+ exports.typeshiLogger = new tslog_1.Logger(exports.MAIN_LOGGER_SETTINGS);
133
+ exports.SIMPLE_LOG_TEMPLATE = ` > `;
111
134
  /** `type: "pretty"`, `template` = `" > {{logObjMeta}}"` */
112
- const SIMPLE_LOGGER_SETTINGS = {
135
+ exports.SIMPLE_LOGGER_SETTINGS = {
113
136
  type: "pretty",
114
137
  name: "typeshi_simple",
115
138
  minLevel: 0,
116
- prettyLogTemplate: SIMPLE_LOG_TEMPLATE,
117
- prettyErrorTemplate: ERROR_TEMPLATE,
118
- prettyErrorStackTemplate: ERROR_STACK_TEMPLATE,
139
+ prettyLogTemplate: exports.SIMPLE_LOG_TEMPLATE,
140
+ prettyErrorTemplate: DEFAULT_ERROR_TEMPLATE,
141
+ prettyErrorStackTemplate: errorStackTemplate,
119
142
  stylePrettyLogs: true,
120
143
  prettyLogTimeZone: "local",
121
- prettyLogStyles: PRETTY_LOG_STYLES,
144
+ prettyLogStyles: exports.PRETTY_LOG_STYLES,
122
145
  };
123
- exports.typeshiSimpleLogger = new tslog_1.Logger(SIMPLE_LOGGER_SETTINGS);
124
- const HIDDEN_LOGGER_SETTINGS = {
146
+ exports.typeshiSimpleLogger = new tslog_1.Logger(exports.SIMPLE_LOGGER_SETTINGS);
147
+ exports.HIDDEN_LOGGER_SETTINGS = {
125
148
  type: "hidden",
126
149
  name: "typeshi_hidden",
127
150
  minLevel: 0,
128
- prettyLogTemplate: SIMPLE_LOG_TEMPLATE,
129
- prettyErrorTemplate: ERROR_TEMPLATE,
130
- prettyErrorStackTemplate: ERROR_STACK_TEMPLATE,
151
+ prettyLogTemplate: exports.SIMPLE_LOG_TEMPLATE,
152
+ prettyErrorTemplate: DEFAULT_ERROR_TEMPLATE,
153
+ prettyErrorStackTemplate: errorStackTemplate,
131
154
  stylePrettyLogs: true,
132
155
  prettyLogTimeZone: "local",
133
- prettyLogStyles: PRETTY_LOG_STYLES,
156
+ prettyLogStyles: exports.PRETTY_LOG_STYLES,
134
157
  };
135
- exports.typeshiHiddenLogger = new tslog_1.Logger(HIDDEN_LOGGER_SETTINGS);
158
+ exports.typeshiHiddenLogger = new tslog_1.Logger(exports.HIDDEN_LOGGER_SETTINGS);
@@ -3,12 +3,15 @@
3
3
  * @description moved the content of parameter type checks at the start of
4
4
  * functions to here. use these when you want your function to throw a fit when
5
5
  * it receives bad input.
6
+ * @example
7
+ * import * as validate from "@typeshi/argumentValidation";
6
8
  * @TODO add boolean value configurable by a setter function that specifies if errors should be thrown or only logged
7
9
  * - maybe add a configurable value that the validation functions should return if the validation test fails
8
10
  * - change the validation functions such that they return the validated value, if possible?
9
11
  * - or maybe have them return boolean type predicates ?
10
12
  * - -> maybe have to make a class
11
13
  * - research the thingy where a type is after the function name and before parens
14
+ * @consideration add a default export named 'validate'
12
15
  */
13
16
  import { TypeOfEnum } from "./typeValidation";
14
17
  /**
@@ -179,7 +182,7 @@ export declare function enumArgument(source: string, label: string, value: any,
179
182
  * = `{ [valueLabel: string]: valueToCheck } & ({ isEnumFunction } | { EnumObject })`
180
183
  */
181
184
  export declare function enumArgument(source: string, labeledArgs: EnumArgumentOptions): string | number;
182
- /** use existingFileArgument() or existingDirectoryArgument() */
185
+ /** recommend to instead use `existingFileArgument()` or `existingDirectoryArgument()` */
183
186
  export declare function existingPathArgument(source: string, arg2: string | {
184
187
  [label: string]: any;
185
188
  }, value?: any, extension?: string): void;
@@ -55,19 +55,21 @@ exports.existingPathArgument = existingPathArgument;
55
55
  * @description moved the content of parameter type checks at the start of
56
56
  * functions to here. use these when you want your function to throw a fit when
57
57
  * it receives bad input.
58
+ * @example
59
+ * import * as validate from "@typeshi/argumentValidation";
58
60
  * @TODO add boolean value configurable by a setter function that specifies if errors should be thrown or only logged
59
61
  * - maybe add a configurable value that the validation functions should return if the validation test fails
60
62
  * - change the validation functions such that they return the validated value, if possible?
61
63
  * - or maybe have them return boolean type predicates ?
62
64
  * - -> maybe have to make a class
63
65
  * - research the thingy where a type is after the function name and before parens
66
+ * @consideration add a default export named 'validate'
64
67
  */
65
68
  const typeValidation_1 = require("./typeValidation");
66
69
  const setupLog_1 = require("../config/setupLog");
67
70
  const regex_1 = require("./regex");
68
71
  const fs = __importStar(require("fs"));
69
72
  const node_path_1 = __importDefault(require("node:path"));
70
- const typeGuardNamePattern = /(?<=^is).*$/i;
71
73
  /**
72
74
  * - {@link isNonEmptyString}`(value: any): value is string & { length: number; }`
73
75
  * @param source `string` indicating what called `stringArgument()`
@@ -313,6 +315,7 @@ function functionArgument(source, arg2, value) {
313
315
  throw new Error(msg);
314
316
  }
315
317
  }
318
+ const typeGuardNamePattern = /(?<=^is).*$/i;
316
319
  /**
317
320
  * @note `if` `elementTypeGuard`'s name or its label includes 'Array' then only check `elementTypeGuard(value)`
318
321
  * instead of checking `value.every(el => elementTypeGuard(el))`
@@ -497,7 +500,7 @@ allowEmpty) {
497
500
  functionLabel = keys.find(k => typeof arg2[k] === 'function');
498
501
  let variableLabel = keys.find(k => typeof arg2[k] !== 'function');
499
502
  if (!variableLabel) {
500
- let msg = [`${source} -> ${vSource} Invalid parameter: arg2 as labeledArgs`,
503
+ let msg = [`${source} -> ${vSource} Invalid parameter: arg2 as ObjectArgumentOptions`,
501
504
  `Expected: object with labeledObject entry and optional labledTypeGuard entry`,
502
505
  `Received: Object.values(arg2) = ${JSON.stringify(Object.values(arg2))}`
503
506
  ].join(setupLog_1.INDENT_LOG_LINE);
@@ -516,7 +519,7 @@ allowEmpty) {
516
519
  else {
517
520
  let msg = [`${source} -> ${vSource} Invalid parameter: 'arg2'`,
518
521
  `Expected: label (string) | labeledArgs (ObjectArgumentOptions | { [label: string]: any | ((value: any) => boolean) })`,
519
- `Received: ${typeof arg2} = ${arg2}`
522
+ `Received: ${typeof arg2} = ${JSON.stringify(arg2)}`
520
523
  ].join(setupLog_1.INDENT_LOG_LINE);
521
524
  setupLog_1.typeshiLogger.error(msg);
522
525
  throw new Error(msg);
@@ -721,7 +724,7 @@ function enumArgument(source, arg2, value, enumLabel, enumObject) {
721
724
  }
722
725
  return matchedValue;
723
726
  }
724
- /** use existingFileArgument() or existingDirectoryArgument() */
727
+ /** recommend to instead use `existingFileArgument()` or `existingDirectoryArgument()` */
725
728
  function existingPathArgument(source, arg2, value, extension) {
726
729
  const vSource = getSourceString(__filename, existingPathArgument.name);
727
730
  source = (0, exports.bracketed)(source);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeshi",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "TypeScript utility modules",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",