@noeldemartin/utils 0.5.0 → 0.5.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noeldemartin/utils",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "My JavaScript utilities",
5
5
  "main": "dist/noeldemartin-utils.cjs.js",
6
6
  "module": "dist/noeldemartin-utils.esm.js",
@@ -42,7 +42,7 @@
42
42
  "@babel/preset-typescript": "^7.12.16",
43
43
  "@microsoft/api-extractor": "^7.12.0",
44
44
  "@noeldemartin/eslint-config-typescript": "^0.1.1",
45
- "@noeldemartin/scripts": "^0.1.2",
45
+ "@noeldemartin/scripts": "^0.2.0",
46
46
  "@rollup/plugin-alias": "^3.1.2",
47
47
  "@rollup/plugin-babel": "^5.2.3",
48
48
  "@rollup/plugin-commonjs": "^17.1.0",
@@ -0,0 +1,38 @@
1
+ export interface FormatCodeBlockOptions {
2
+ indent?: number;
3
+ }
4
+
5
+ export function formatCodeBlock(code: string, options: FormatCodeBlockOptions = {}): string {
6
+ const lines = code.split('\n');
7
+ const indent = options.indent ?? 0;
8
+ let originalIndent = 0;
9
+ let formatted = '';
10
+
11
+ for (const line of lines) {
12
+ const trimmedLine = line.trim();
13
+ const isEmptyLine = trimmedLine.length === 0;
14
+
15
+ if (formatted.length === 0) {
16
+ if (isEmptyLine) {
17
+ continue;
18
+ }
19
+
20
+ originalIndent = line.indexOf(trimmedLine[0] ?? '');
21
+ formatted += `${' '.repeat(indent)}${trimmedLine}\n`;
22
+
23
+ continue;
24
+ }
25
+
26
+ if (isEmptyLine) {
27
+ formatted += '\n';
28
+
29
+ continue;
30
+ }
31
+
32
+ const lineIndent = line.indexOf(trimmedLine[0] ?? '');
33
+
34
+ formatted += `${' '.repeat(indent + lineIndent - originalIndent)}${trimmedLine}\n`;
35
+ }
36
+
37
+ return formatted.trimEnd();
38
+ }
@@ -17,6 +17,7 @@ export * from './date_helpers';
17
17
  export * from './error_helpers';
18
18
  export * from './escape_helpers';
19
19
  export * from './file_helpers';
20
+ export * from './format_helpers';
20
21
  export * from './hash_helpers';
21
22
  export * from './identity_helpers';
22
23
  export * from './location_helpers';
@@ -151,10 +151,11 @@ export function objectMap<T>(items: T[], property: KeyOf<T, number | string>): R
151
151
  }, {} as Record<string, T>);
152
152
  }
153
153
 
154
- export function objectOnly<T extends Obj, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
154
+ export function objectOnly<T extends object, K extends keyof T>(obj: T, keys: K | K[]): Pick<T, K> {
155
155
  const newObject: Pick<T, K> = {} as Pick<T, K>;
156
+ const keysArray = Array.isArray(keys) ? keys : [keys];
156
157
 
157
- for (const key of keys) {
158
+ for (const key of keysArray) {
158
159
  if (!(key in obj))
159
160
  continue;
160
161
 
@@ -41,6 +41,12 @@ export function debounce<Args extends ClosureArgs>(
41
41
  return debouncedCallback;
42
42
  }
43
43
 
44
+ export async function forever(): Promise<void> {
45
+ await new Promise(() => {
46
+ // Nothing to do here.
47
+ });
48
+ }
49
+
44
50
  export function isValidDateString(value: string): boolean {
45
51
  if (typeof value !== 'string')
46
52
  return false;