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