@dolthub/web-utils 0.1.0 → 0.1.3

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,7 +1,26 @@
1
+ /// <reference types="node" />
2
+ import { Select, ColumnRef } from 'node-sql-parser';
3
+ import { ParsedUrlQueryInput } from 'querystring';
4
+ import { UrlObject } from 'url';
5
+
1
6
  type Maybe<T> = T | undefined | null;
2
7
 
3
8
  declare function compareArray(arr1: string[], arr2: string[]): boolean;
4
9
 
10
+ declare const oneHour: number;
11
+ declare function getUTCDateAndTimeString(date: Date): string;
12
+ declare function getDateString(date: Date): string;
13
+ declare function getUTCDateString(date: Date): string;
14
+ declare function getUTCTimeString(date: Date): string;
15
+ declare function getNow(): Date;
16
+ declare function getDateMinusHours(date: Date, hours: number): Date;
17
+ declare function getUTCNowDateString(): string;
18
+ declare function getLongDateTimeString(d: Date): string;
19
+ declare function getTimeWithSeconds(date: Date): string;
20
+ declare function getTimeAgoString(oldDateTime: number): string;
21
+ declare function areTimeAgosEqual(a: Date, b: Date): boolean;
22
+ declare function convertToUTCDate(d: Date): Date;
23
+
5
24
  declare function dedupe<T>(arr: T[]): T[];
6
25
 
7
26
  /**
@@ -25,20 +44,281 @@ declare function dedupe<T>(arr: T[]): T[];
25
44
  */
26
45
  declare function enumKeys<O extends object, K extends keyof O = keyof O>(obj: O): K[];
27
46
 
28
- declare const _default: (str: string, len?: number) => string;
47
+ declare function excerpt(str: string, len?: number): string;
48
+
49
+ declare function fakeEscapePress(): void;
29
50
 
30
51
  declare function initialUppercase(s: string): string;
31
52
 
53
+ declare const loremer: {
54
+ word: ({ trueSingleWord, minLength, maxLength, matchRegex, }?: {
55
+ trueSingleWord?: boolean | undefined;
56
+ matchRegex?: RegExp | undefined;
57
+ minLength?: number | undefined;
58
+ maxLength?: number | undefined;
59
+ }) => string;
60
+ words: (n: number) => string[];
61
+ resourceNameSegment: () => string;
62
+ sentence: (options?: {
63
+ max?: number | undefined;
64
+ min?: number | undefined;
65
+ }) => string;
66
+ sentences: (n: number) => string[];
67
+ paragraph: () => string;
68
+ };
69
+
32
70
  declare function nTimes<T>(n: number, operation: (...args: any[]) => T, args?: Parameters<typeof operation>): T[];
33
71
  declare function nTimesWithIndex<T>(n: number, operation: (n: number) => T): T[];
34
72
 
35
- declare function export_default(data: any): string;
73
+ declare const NULL_VALUE = "\uF5F2\uEB94NULL\uF5A8\uE6FF";
74
+ declare function isNullValue(dv: string): boolean;
75
+ declare function getDisplayValue(dv: string): string;
76
+ declare function getDisplayValueForApi(dv: string): string | null;
77
+
78
+ declare function formatNumber(num: number): string;
79
+ declare function formatToUsd(num: number): string;
80
+ declare function formatToRoundedUsd(num: number): string;
81
+
82
+ type TableColumn = {
83
+ name: string;
84
+ sourceTable?: Maybe<string>;
85
+ };
86
+ declare function parseSelectQuery(q: string): Select | null;
87
+ declare function requireTableNameForSelect(q: string): string[];
88
+ declare function getTableName(q?: string): Maybe<string>;
89
+ type Column = {
90
+ expr: ColumnRef;
91
+ as: string | null;
92
+ };
93
+ type Columns = any[] | "*" | Column[];
94
+ declare function getColumns(q: string): Columns | undefined;
95
+ declare function convertToSqlWithNewColNames(q: string, cols: string[] | "*", tableName: string): string;
96
+ declare function convertToSqlWithNewCols(q: string, cols: TableColumn[] | "*", tableNames?: string[]): string;
97
+ declare function convertToSqlWithNewCondition(query: string, column: string, value: string): string;
98
+ declare function convertToSqlWithOrderBy(query: string, column: string, type?: "ASC" | "DESC"): string;
99
+ declare function queryHasOrderBy(query: string, column: string, type?: "ASC" | "DESC"): boolean;
100
+ declare function isMutation(q?: string): boolean;
101
+ declare function removeColumnFromQuery(q: string, colNameToRemove: string, cols: TableColumn[]): string;
102
+ declare function makeQueryExecutable(q: string): string;
103
+ declare function isMultipleQueries(queryString: string): boolean;
104
+
105
+ declare function pluralize(num: number, str: string): string;
106
+
107
+ declare function prettyJSON(data: any): string;
36
108
  declare function prettyJSONText(text: string): string;
37
109
 
38
110
  declare function randomArrayItem<T>(arr: T[]): T;
39
111
 
112
+ declare function randomNum(min: number, max: number): number;
113
+
40
114
  declare function safeJSONParse(text: string): any;
41
115
 
42
116
  declare function tuplify<T extends any[]>(...elements: T): T;
43
117
 
44
- export { type Maybe, compareArray, dedupe, enumKeys, _default as excerpt, initialUppercase, nTimes, nTimesWithIndex, export_default as prettyJSON, prettyJSONText, randomArrayItem, safeJSONParse, tuplify };
118
+ type Url = string | UrlObject;
119
+ /**
120
+ * __Route__
121
+ *
122
+ * `Route` class that handles adding static and dynamic route segments to
123
+ * existing routes. `Route` can be initialized with a "href" string or
124
+ * `UrlObject` and optional "as" string or `UrlObject`.
125
+ *
126
+ * @example
127
+ * const routeA = new Route("/repositories");
128
+ * routeA.hrefPathname() // "/repositories"
129
+ * routeA.addDynamic("ownerName", "my-owner")
130
+ * routeA.hrefPathname() // "repositories/[ownerName]"
131
+ * routeA.asPathname() // "repositories/my-owner"
132
+ *
133
+ * const routeB = new Route(
134
+ * { pathname: "/repositories/[ownerName]", query: { refName: "main" } },
135
+ * { pathname: "/repositories/my-owner", query: { refName: "main" } },
136
+ * )
137
+ * routeB.hrefPathname() // "/repositories/[ownerName]"
138
+ * routeB.asPathname() // "repositories/my-owner"
139
+ * routeB.getQuery() // { refName: "main" }
140
+ */
141
+ declare class Route {
142
+ href: Url;
143
+ as: Url;
144
+ constructor(href: Url, as?: Url);
145
+ /**
146
+ * __hrefPathname__
147
+ *
148
+ * Gets pathname string from href string or `UrlObject`.
149
+ *
150
+ * @param addFrag Optional route fragment string to add to `href` pathname
151
+ *
152
+ * @returns `href` pathname string, with added route fragment if provided
153
+ *
154
+ * @example
155
+ * const routeA = new Route("/repositories");
156
+ * routeA.hrefPathname() // "/repositories"
157
+ * routeA.hrefPathname("[ownerName]") // "repositories/[ownerName]"
158
+ *
159
+ * const routeB = new Route({ pathname: "/repositories" })
160
+ * routeB.hrefPathname() // "/repositories"
161
+ * routeB.hrefPathname("[ownerName]") // "repositories/[ownerName]"
162
+ */
163
+ hrefPathname(addFrag?: string): string;
164
+ /**
165
+ * __asPathname__
166
+ *
167
+ * Gets pathname string from as string or `UrlObject`.
168
+ *
169
+ * @param addFrag Optional route fragment string to add to `as` pathname
170
+ *
171
+ * @returns `as` pathname string, with added route fragment if provided
172
+ *
173
+ * @example
174
+ * const routeA = new Route("/repositories");
175
+ * routeA.asPathname() // "/repositories"
176
+ * routeA.asPathname("my-username") // "repositories/my-username"
177
+ *
178
+ * const routeB = new Route(
179
+ * { pathname: "/repositories/[ownerName]" },
180
+ * { pathname: "/repositories/my-username" },
181
+ * );
182
+ * routeB.asPathname() // "/repositories/my-username"
183
+ * routeB.asPathname("data") // "repositories/my-username/data"
184
+ */
185
+ asPathname(addFrag?: string): string;
186
+ /**
187
+ * __getQuery__
188
+ *
189
+ * Gets query object from the href `UrlObject`.
190
+ *
191
+ * @returns query object
192
+ *
193
+ * @example
194
+ * const route = new Route("/repositories");
195
+ * route.withQuery({ refName: "main" })
196
+ * route.getQuery() // { refName: "main" }
197
+ */
198
+ getQuery(): string | ParsedUrlQueryInput | undefined | null;
199
+ /**
200
+ * __getHash__
201
+ *
202
+ * Gets hash string from the href `UrlObject`.
203
+ *
204
+ * @returns hash string
205
+ *
206
+ * @example
207
+ * const route = new Route("/repositories");
208
+ * route.withHash("commitA")
209
+ * route.getHash() // "commitA"
210
+ */
211
+ getHash(): string;
212
+ /**
213
+ * __addStatic__
214
+ *
215
+ * Adds static route fragment string to a `Route`. Static route fragments look like:
216
+ * ```ts
217
+ * { href: "/repositories", as: "/repositories" }
218
+ * ```
219
+ * As opposed to dynamic route fragments which look like:
220
+ * ```ts
221
+ * { href: "/[ownerName]", as: "/owner-value" }
222
+ * ```
223
+ *
224
+ * @param pathFrag Route fragment string to add to the url. "/" is automatically prepended.
225
+ *
226
+ * @returns new instance of `Route`
227
+ *
228
+ * @example
229
+ * const route = new Route("/repositories");
230
+ * route.addStatic("data")
231
+ * route.hrefPathname() // "/repositories/data"
232
+ * route.asPathname() // "/repositories/data"
233
+ */
234
+ addStatic(pathFrag: string): Route;
235
+ /**
236
+ * __maybeAddStatic__
237
+ *
238
+ * Maybe adds static route fragment string to a `Route`.
239
+ *
240
+ * @see this.addStatic
241
+ *
242
+ * @param pathFrag Optional route fragment string to add to the url. "/" is automatically prepended.
243
+ *
244
+ * @example
245
+ * const route = new Route("/repositories");
246
+ * route.maybeAddStatic("data")
247
+ * route.hrefPathname() // "/repositories/data"
248
+ * route.maybeAddStatic()
249
+ * route.hrefPathname() // "/repositories"
250
+ */
251
+ maybeAddStatic(pathFrag?: string): Route;
252
+ /**
253
+ * __addDynamic__
254
+ *
255
+ * Adds dynamic route fragment string to a Route.
256
+ * Dynamic route fragments look like:
257
+ * ```ts
258
+ * { href: "/[ownerName]", as: "/owner-value" }
259
+ * ```
260
+ * As opposed to static route fragments which look like:
261
+ *
262
+ * ```ts
263
+ * { href: "/repositories", as: "/repositories" }
264
+ * ```
265
+ *
266
+ * @param param String fragment to add to the href path. This value will be wrapped in square brackets. "/" is automatically prepended.
267
+ * @param as Value of the `param` to be added to the `as` path. "/" is automatically prepended.
268
+ * @param encode Default false. If true, encodes a `as` param value string as a valid component of a Uniform Resource Identifier (URI).
269
+ *
270
+ * @returns new instance of `Route`
271
+ *
272
+ * @example
273
+ * const route = new Route("/repositories");
274
+ * route.addDynamic("ownerName", "my-owner-name")
275
+ * route.hrefPathname() // "/repositories/[ownerName]"
276
+ * route.asPathname() // "/repositories/my-owner-name"
277
+ */
278
+ addDynamic(param: string, as: string, encode?: boolean): Route;
279
+ /**
280
+ * __withQuery__
281
+ *
282
+ * Adds query object to a `Route`. Looks like "?refName=main&active=Tables"
283
+ * when url is formatted.
284
+ *
285
+ * @param q Record with fields that will be added to the query string.
286
+ *
287
+ * @returns new instance of `Route`
288
+ *
289
+ * @example
290
+ * const route = new Route("/repositories/[ownerName]", "/repositories/my-owner");
291
+ * route.withQuery({ refName: "main", active: undefined })
292
+ * route.hrefPathname() // { pathname: "/repositories/[ownerName]", query: { refName: "main" } }
293
+ * route.asPathname() // { pathname: "/repositories/my-owner", query: { refName: "main" } }
294
+ */
295
+ withQuery(q: Record<string, string | undefined | null>): Route;
296
+ /**
297
+ * __withHash__
298
+ *
299
+ * Adds hash string to a `Route`. Looks like "#hash-value" when url is formatted.
300
+ *
301
+ * @param hash Optional hash string
302
+ *
303
+ * @returns new instance of Route
304
+ *
305
+ * @example
306
+ * const route = new Route("/repositories/[ownerName]", "/repositories/my-owner");
307
+ * route.withHash({ commitId: "commitA" })
308
+ * route.hrefPathname() // { pathname: "/repositories/[ownerName]", hash: { commitId: "commitA" } }
309
+ * route.asPathname() // { pathname: "/repositories/my-owner", hash: { commitId: "commitA" } }
310
+ */
311
+ withHash(hash?: string): Route;
312
+ }
313
+
314
+ declare function emailValidator(s: string): {
315
+ isValid: boolean;
316
+ errorMsg: string;
317
+ };
318
+ declare function usernameValidator(s: string): {
319
+ isValid: boolean;
320
+ errorMsg: string;
321
+ detailedErrorMsg: string;
322
+ };
323
+
324
+ export { type Maybe, NULL_VALUE, Route, areTimeAgosEqual, compareArray, convertToSqlWithNewColNames, convertToSqlWithNewCols, convertToSqlWithNewCondition, convertToSqlWithOrderBy, convertToUTCDate, dedupe, emailValidator, enumKeys, excerpt, fakeEscapePress, formatNumber, formatToRoundedUsd, formatToUsd, getColumns, getDateMinusHours, getDateString, getDisplayValue, getDisplayValueForApi, getLongDateTimeString, getNow, getTableName, getTimeAgoString, getTimeWithSeconds, getUTCDateAndTimeString, getUTCDateString, getUTCNowDateString, getUTCTimeString, initialUppercase, isMultipleQueries, isMutation, isNullValue, loremer, makeQueryExecutable, nTimes, nTimesWithIndex, oneHour, parseSelectQuery, pluralize, prettyJSON, prettyJSONText, queryHasOrderBy, randomArrayItem, randomNum, removeColumnFromQuery, requireTableNameForSelect, safeJSONParse, tuplify, usernameValidator };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@dolthub/web-utils",
3
3
  "author": "DoltHub",
4
- "version": "0.1.0",
4
+ "description": "A collection of utilities for building web applications",
5
+ "version": "0.1.3",
5
6
  "main": "dist/cjs/index.js",
6
7
  "module": "dist/esm/index.js",
7
8
  "types": "dist/index.d.ts",
@@ -21,7 +22,7 @@
21
22
  "lint": "eslint --cache --ext .ts,.js,.tsx,.jsx src",
22
23
  "prettier": "prettier --check 'src/**/*.{js,ts}'",
23
24
  "prettier-fix": "prettier --write 'src/**/*.{js,ts}'",
24
- "publish": "yarn dbuild && npm publish",
25
+ "npm:publish": "yarn dbuild && npm publish",
25
26
  "test": "jest",
26
27
  "yalc:publish": "yarn dbuild && yalc publish",
27
28
  "yalc:push": "yarn dbuild && yalc push"
@@ -55,5 +56,9 @@
55
56
  },
56
57
  "bugs": {
57
58
  "url": "https://github.com/dolthub/react-library/issues"
59
+ },
60
+ "dependencies": {
61
+ "node-sql-parser": "^4.17.0",
62
+ "timeago.js": "^4.0.2"
58
63
  }
59
64
  }