UNPKG

14.8 kBMarkdownView Raw
1# @cumulus/common API Documentation
2
3## Modules
4
5<dl>
6<dt><a href="#module_string">string</a></dt>
7<dd><p>A collection of utilities for working with URLs</p>
8</dd>
9<dt><a href="#module_URLUtils">URLUtils</a></dt>
10<dd><p>A collection of utilities for working with URLs</p>
11</dd>
12<dt><a href="#module_util">util</a></dt>
13<dd><p>Simple utility functions</p>
14</dd>
15</dl>
16
17<a name="module_string"></a>
18
19## string
20A collection of utilities for working with URLs
21
22**Example**
23```js
24const { match } = require('@cumulus/common/string');
25
26match(/aS/, aSDf');
27```
28
29* [string](#module_string)
30 * ~~[replace(pattern, replacement, string)](#exp_module_string--replace) ⇒ <code>string</code> ⏏~~
31 * ~~[globalReplace(string, oldSubString, newSubString)](#exp_module_string--globalReplace) ⇒ <code>string</code> ⏏~~
32 * ~~[toLower(str)](#exp_module_string--toLower) ⇒ <code>string</code> ⏏~~
33 * ~~[toUpper(str)](#exp_module_string--toUpper) ⇒ <code>string</code> ⏏~~
34 * ~~[match(regexp, str)](#exp_module_string--match) ⇒ <code>Array</code> \| <code>null</code> ⏏~~
35 * ~~[matches(regexp, str)](#exp_module_string--matches) ⇒ <code>boolean</code> ⏏~~
36 * ~~[isValidHostname(hostname)](#exp_module_string--isValidHostname) ⇒ <code>boolean</code> ⏏~~
37 * ~~[isNonEmptyString(x)](#exp_module_string--isNonEmptyString) ⇒ <code>boolean</code> ⏏~~
38
39<a name="exp_module_string--replace"></a>
40
41### ~~replace(pattern, replacement, string) ⇒ <code>string</code> ⏏~~
42***Deprecated***
43
44Return a new string with some or all matches of a pattern replaced by a
45replacement.
46
47**Kind**: Exported function
48**Returns**: <code>string</code> - the modified string
49
50For additional details on the pattern and replacement arguments, see:
51 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Parameters
52
53This is a curried function - https://lodash.com/docs/4.17.11#curry
54
55| Param | Type | Description |
56| --- | --- | --- |
57| pattern | <code>string</code> \| <code>RegExp</code> | if a string, this is the substring to be replaced by `replacement`. If a RegExp, any match or matches will be replaced by `replacement`. |
58| replacement | <code>string</code> \| <code>function</code> | if a string, the value to replace `pattern` with. If a function, instances of `pattern` will be replaced with the result of calling the function. |
59| string | <code>string</code> | The string to modify |
60
61<a name="exp_module_string--globalReplace"></a>
62
63### ~~globalReplace(string, oldSubString, newSubString) ⇒ <code>string</code> ⏏~~
64***Deprecated***
65
66Globally replaces oldSubstring in string with newSubString
67
68**Kind**: Exported function
69**Returns**: <code>string</code> - the modified string
70
71| Param | Type | Description |
72| --- | --- | --- |
73| string | <code>string</code> | The string to modify |
74| oldSubString | <code>string</code> | The string to replace |
75| newSubString | <code>string</code> | The string replacement |
76
77<a name="exp_module_string--toLower"></a>
78
79### ~~toLower(str) ⇒ <code>string</code> ⏏~~
80***Deprecated***
81
82Converts string, as a whole, to lower case just like String#toLowerCase
83
84**Kind**: Exported function
85**Returns**: <code>string</code> - the lower-cased string
86
87| Param | Type | Description |
88| --- | --- | --- |
89| str | <code>string</code> | the string to convert |
90
91<a name="exp_module_string--toUpper"></a>
92
93### ~~toUpper(str) ⇒ <code>string</code> ⏏~~
94***Deprecated***
95
96Converts string, as a whole, to upper case just like String#toUpperCase
97
98**Kind**: Exported function
99**Returns**: <code>string</code> - the upper-cased string
100
101| Param | Type | Description |
102| --- | --- | --- |
103| str | <code>string</code> | the string to convert |
104
105<a name="exp_module_string--match"></a>
106
107### ~~match(regexp, str) ⇒ <code>Array</code> \| <code>null</code> ⏏~~
108***Deprecated***
109
110Tests a regular expression against a String, returning matches
111
112Produces same output as https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
113
114This is a curried function - https://lodash.com/docs/4.17.11#curry
115
116**Kind**: Exported function
117
118| Param | Type | Description |
119| --- | --- | --- |
120| regexp | <code>RegExp</code> | the pattern to match against |
121| str | <code>string</code> | the string to match against |
122
123<a name="exp_module_string--matches"></a>
124
125### ~~matches(regexp, str) ⇒ <code>boolean</code> ⏏~~
126***Deprecated***
127
128Tests a regular expression against a string, returning true / false
129
130This is a curried function - https://lodash.com/docs/4.17.11#curry
131
132**Kind**: Exported function
133**Returns**: <code>boolean</code> - true if the pattern matches the string, false otherwise
134
135| Param | Type | Description |
136| --- | --- | --- |
137| regexp | <code>RegExp</code> | the pattern to match against |
138| str | <code>string</code> | the string to match against |
139
140**Example**
141```js
142const isCapitalized = matches(/^[A-Z]/);
143isCapitalized('Joe'); // => true
144```
145<a name="exp_module_string--isValidHostname"></a>
146
147### ~~isValidHostname(hostname) ⇒ <code>boolean</code> ⏏~~
148***Deprecated***
149
150Test if a string is a valid hostname, as defined by [RFC1123](https://tools.ietf.org/html/rfc1123#page-13)
151
152**Kind**: Exported function
153
154| Param | Type | Description |
155| --- | --- | --- |
156| hostname | <code>String</code> | the string to test |
157
158**Example**
159```js
160isValidHostname('example.com'); // => true
161isValidHostname('as!@#'); // => false
162isValidHostname('127.0.0.1'); // => false
163```
164<a name="exp_module_string--isNonEmptyString"></a>
165
166### ~~isNonEmptyString(x) ⇒ <code>boolean</code> ⏏~~
167***Deprecated***
168
169Test if a value is a string with a length greater than zero
170
171**Kind**: Exported function
172
173| Param | Type | Description |
174| --- | --- | --- |
175| x | <code>string</code> | the string to test |
176
177<a name="module_URLUtils"></a>
178
179## URLUtils
180A collection of utilities for working with URLs
181
182**Example**
183```js
184const { buildURL } = require('@cumulus/common/URLUtils');
185
186buildURL({ protocol: 'http', host: 'example.com' }); // => 'http://example.com'
187```
188<a name="exp_module_URLUtils--buildURL"></a>
189
190### buildURL(params) ⇒ <code>string</code> ⏏
191Build a URL
192
193**Kind**: Exported function
194**Returns**: <code>string</code> - a URL
195**Throws**:
196
197- <code>TypeError</code> if protocol or host are not specified
198
199
200| Param | Type | Description |
201| --- | --- | --- |
202| params | <code>Object</code> | URL parameters |
203| params.protocol | <code>string</code> | the protocol ('http', 'ftp', 's3', etc) |
204| params.host | <code>string</code> | the host |
205| [params.port] | <code>string</code> \| <code>integer</code> | the port |
206| [params.path] | <code>string</code> \| <code>Array.&lt;string&gt;</code> | path segment(s) to add to the end of the URL. Can be either a string or an array of strings, which will be joined together. |
207
208**Example**
209```js
210buildURL({
211 protocol: 'http'
212 host: 'example.com',
213 port: 8080,
214 path: ['path', 'to', 'file.txt']
215}); // => 'http://example.com:8080/path/to/file.txt'
216```
217<a name="module_util"></a>
218
219## util
220Simple utility functions
221
222**Example**
223```js
224const { isNil } = require('@cumulus/common/util');
225
226isNil(undefined); // => true
227```
228
229* [util](#module_util)
230 * [exports.deprecate(name, version, [alternative])](#exp_module_util--exports.deprecate) ⏏
231 * [exports.sleep(waitPeriodMs)](#exp_module_util--exports.sleep) ⇒ <code>Promise.&lt;undefined&gt;</code>
232 * [exports.uuid()](#exp_module_util--exports.uuid) ⇒ <code>string</code>
233 * [exports.noop()](#exp_module_util--exports.noop) ⇒ <code>undefined</code>
234 * [exports.omit(objectIn, keys)](#exp_module_util--exports.omit) ⇒ <code>Object</code>
235 * [exports.negate(predicate)](#exp_module_util--exports.negate) ⇒ <code>function</code>
236 * [exports.isNull(x)](#exp_module_util--exports.isNull) ⇒ <code>boolean</code>
237 * [exports.isUndefined(x)](#exp_module_util--exports.isUndefined) ⇒ <code>boolean</code>
238 * [exports.isNil(x)](#exp_module_util--exports.isNil) ⇒ <code>boolean</code>
239 * ~~[exports.setErrorStack(error, newStack)](#exp_module_util--exports.setErrorStack) ⏏~~
240 * [exports.renameProperty(from, to, obj)](#exp_module_util--exports.renameProperty) ⇒ <code>Object</code>
241 * [exports.removeNilProperties(obj)](#exp_module_util--exports.removeNilProperties) ⇒ <code>Object</code>
242 * [exports.lookupMimeType(key)](#exp_module_util--exports.lookupMimeType) ⇒ <code>string</code>
243 * [exports.isOneOf(collection, val)](#exp_module_util--exports.isOneOf) ⇒ <code>boolean</code>
244 * [exports.thread(value, ...fns)](#exp_module_util--exports.thread) ⇒ <code>\*</code>
245
246<a name="exp_module_util--exports.deprecate"></a>
247
248### exports.deprecate(name, version, [alternative]) ⏏
249Mark a piece of code as deprecated
250
251**Kind**: Exported function
252
253| Param | Type | Description |
254| --- | --- | --- |
255| name | <code>string</code> | the name of the function / method / class to deprecate |
256| version | <code>string</code> | the version after which the code will be marked as deprecated |
257| [alternative] | <code>string</code> | the function / method / class to use instead of this deprecated code |
258
259<a name="exp_module_util--exports.sleep"></a>
260
261### exports.sleep(waitPeriodMs) ⇒ <code>Promise.&lt;undefined&gt;</code> ⏏
262Wait for the defined number of milliseconds
263
264**Kind**: Exported function
265**Returns**: <code>Promise.&lt;undefined&gt;</code> - promise resolves after a given time period
266
267| Param | Type | Description |
268| --- | --- | --- |
269| waitPeriodMs | <code>number</code> | number of milliseconds to wait |
270
271<a name="exp_module_util--exports.uuid"></a>
272
273### exports.uuid() ⇒ <code>string</code> ⏏
274Generate and return an RFC4122 v4 UUID.
275
276**Kind**: Exported function
277**Returns**: <code>string</code> - An RFC44122 v4 UUID.
278<a name="exp_module_util--exports.noop"></a>
279
280### exports.noop() ⇒ <code>undefined</code> ⏏
281Does nothing. Used where a callback is required but not used.
282
283**Kind**: Exported function
284**Returns**: <code>undefined</code> - undefined
285<a name="exp_module_util--exports.omit"></a>
286
287### exports.omit(objectIn, keys) ⇒ <code>Object</code> ⏏
288Replacement for lodash.omit returns a shallow copy of input object
289with keys removed.
290(lodash.omit will be removed in v5.0.0)
291https://github.com/lodash/lodash/wiki/Roadmap#v500-2019
292
293**Kind**: Exported function
294**Returns**: <code>Object</code> - copy of objectIn without keys attached.
295
296| Param | Type | Description |
297| --- | --- | --- |
298| objectIn | <code>Object</code> | input object |
299| keys | <code>string</code> \| <code>Array.&lt;string&gt;</code> | key or list of keys to remove from object |
300
301<a name="exp_module_util--exports.negate"></a>
302
303### exports.negate(predicate) ⇒ <code>function</code> ⏏
304Creates a function that returns the opposite of the predicate function.
305
306**Kind**: Exported function
307**Returns**: <code>function</code> - the new negated function
308
309| Param | Type | Description |
310| --- | --- | --- |
311| predicate | <code>function</code> | the predicate to negate |
312
313**Example**
314```js
315const isEven = (x) => x % 2 === 0;
316const isOdd = negate(isEven);
317
318isOdd(2); // => false
319isOdd(3); // => true
320```
321<a name="exp_module_util--exports.isNull"></a>
322
323### exports.isNull(x) ⇒ <code>boolean</code> ⏏
324Test if a value is null
325
326**Kind**: Exported function
327
328| Param | Type | Description |
329| --- | --- | --- |
330| x | <code>\*</code> | value to check |
331
332<a name="exp_module_util--exports.isUndefined"></a>
333
334### exports.isUndefined(x) ⇒ <code>boolean</code> ⏏
335Test if a value is undefined
336
337**Kind**: Exported function
338
339| Param | Type | Description |
340| --- | --- | --- |
341| x | <code>\*</code> | value to check |
342
343<a name="exp_module_util--exports.isNil"></a>
344
345### exports.isNil(x) ⇒ <code>boolean</code> ⏏
346Test if a value is null or undefined
347
348**Kind**: Exported function
349
350| Param | Type | Description |
351| --- | --- | --- |
352| x | <code>\*</code> | value to check |
353
354<a name="exp_module_util--exports.setErrorStack"></a>
355
356### ~~exports.setErrorStack(error, newStack) ⏏~~
357***Deprecated***
358
359Replace the stack of an error
360
361Note: This mutates the error that was passed in.
362
363**Kind**: Exported function
364
365| Param | Type | Description |
366| --- | --- | --- |
367| error | <code>Error</code> | an Error |
368| newStack | <code>string</code> | a stack trace |
369
370<a name="exp_module_util--exports.renameProperty"></a>
371
372### exports.renameProperty(from, to, obj) ⇒ <code>Object</code> ⏏
373Rename an object property
374
375**Kind**: Exported function
376**Returns**: <code>Object</code> - a shallow clone of the object with updated property name
377
378| Param | Type | Description |
379| --- | --- | --- |
380| from | <code>string</code> | old property name |
381| to | <code>string</code> | new property name |
382| obj | <code>Object</code> | object to update |
383
384<a name="exp_module_util--exports.removeNilProperties"></a>
385
386### exports.removeNilProperties(obj) ⇒ <code>Object</code> ⏏
387Remove properties whose values are `null` or `undefined`
388
389**Kind**: Exported function
390**Returns**: <code>Object</code> - a shallow clone of the object with `null` and `undefined`
391 properties removed
392
393| Param | Type | Description |
394| --- | --- | --- |
395| obj | <code>Object</code> | object to update |
396
397<a name="exp_module_util--exports.lookupMimeType"></a>
398
399### exports.lookupMimeType(key) ⇒ <code>string</code> ⏏
400Return mime-type based on input url or filename
401
402**Kind**: Exported function
403**Returns**: <code>string</code> - mimeType or null
404
405| Param | Type |
406| --- | --- |
407| key | <code>string</code> |
408
409<a name="exp_module_util--exports.isOneOf"></a>
410
411### exports.isOneOf(collection, val) ⇒ <code>boolean</code> ⏏
412Test if a value is included in a list of items
413
414This is a curried function - https://lodash.com/docs/4.17.11#curry
415
416**Kind**: Exported function
417
418| Param | Type | Description |
419| --- | --- | --- |
420| collection | <code>Array</code> | the list of items to check against |
421| val | <code>Object</code> | the item to check for in the collection |
422
423<a name="exp_module_util--exports.thread"></a>
424
425### exports.thread(value, ...fns) ⇒ <code>\*</code> ⏏
426Pass a value through a pipeline of functions and return the result
427
428**Kind**: Exported function
429**Returns**: <code>\*</code> - the result of passing the value through the functions:
430 - If no functions are provided, the value is returned.
431 - Functions should expect a single argument
432
433| Param | Type | Description |
434| --- | --- | --- |
435| value | <code>\*</code> | the value to be passed through the pipeline of functions |
436| ...fns | <code>function</code> | the functions to be invoked |
437
438
439---
440
441Generated automatically using `npm run build-docs`