Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit db234a5

Browse files
committed
Add toURL & fromURL
1 parent 9990d30 commit db234a5

9 files changed

+122
-33
lines changed

README.md

+60-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [mellotron](https://www.npmjs.com/package/mellotron) *0.1.6*
1+
# [mellotron](https://www.npmjs.com/package/mellotron) *0.1.7*
22

33
> Synthetic string orchestra. Curated list of string manipulation curried functions
44
```
@@ -8,6 +8,7 @@
88
npm install mellotron
99
```
1010

11+
## Methods
1112
> [camelCase](#camelCase)
1213
> [capitalize](#capitalize)
1314
> [concat](#concat)
@@ -34,11 +35,11 @@
3435
> [toQuery](#toQuery)
3536
> [toString](#toString)
3637
> [toUpper](#toUpper)
38+
> [toURL](#toURL)
3739
> [trim](#trim)
3840
> [trimLeft](#trimLeft)
3941
> [trimRight](#trimRight)
4042
41-
4243
<a id="camelCase"></a>
4344
## camelCase([string&#x3D;&#x27;&#x27;])
4445
Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase).
@@ -208,7 +209,7 @@ template(['David', 'Chambers']) // => 'The name's Chambers. David Chambers.'
208209

209210
<a id="fromQuery"></a>
210211
## fromQuery(str[, sep&#x3D;&#x27;&amp;&#x27;, eq&#x3D;&#x27;&#x3D;&#x27;, options])
211-
Parse a query string into an object. Leading ? or # are ignored, so you can pass location.search or location.hash directly.
212+
Parse a query string into an object.
212213
From [querystring/parse](https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options)
213214

214215

@@ -219,11 +220,11 @@ From [querystring/parse](https://nodejs.org/api/querystring.html#querystring_que
219220
| Name | Type | Description | |
220221
| ---- | ---- | ----------- | -------- |
221222
| str | `string` | The URL query string to parse. | &nbsp; |
222-
| sep&#x3D;&#x27;&amp;&#x27; | `string` | The substring used to delimit key and value pairs in the query string. Defaults to &#x27;&amp;&#x27;. | *Optional* |
223-
| eq&#x3D;&#x27;&#x3D;&#x27; | `string` | The substring used to delimit keys and values in the query string. Defaults to &#x27;&#x3D;&#x27;. | *Optional* |
223+
| sep&#x3D;&#x27;&amp;&#x27; | `string` | The substring to delimit key and value pairs. | *Optional* |
224+
| eq&#x3D;&#x27;&#x3D;&#x27; | `string` | The substring to delimit keys and values. | *Optional* |
224225
| options | `Object` | | *Optional* |
225-
| options.decodeURIComponent&#x3D;querystring.unescape() | `Function` | The function to use when decoding percent-encoded characters in the query string. | *Optional* |
226-
| options.maxKeys&#x3D;1000 | `Function` | Specifies the maximum number of keys to parse. Defaults to 1000. Specify 0 to remove key counting limitations. | *Optional* |
226+
| options.decodeURIComponent&#x3D;querystring.unescape() | `Function` | The decoding function. | *Optional* |
227+
| options.maxKeys&#x3D;1000 | `Function` | Maximum number of keys to parse. 0 to remove key counting limitations. | *Optional* |
227228

228229

229230
##### Examples
@@ -241,7 +242,7 @@ fromQuery('foo:1|foo:2|foo:3', '|', ':');
241242
<a id="fromURL"></a>
242243
## fromURL(urlString[, parseQueryString&#x3D;false, slashesDenoteHost&#x3D;false])
243244
Parse a query string into an object. Leading ? or # are ignored, so you can pass location.search or location.hash directly.
244-
From [querystring/parse](https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options)
245+
From [url/parse](https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost)
245246

246247

247248

@@ -251,23 +252,32 @@ From [querystring/parse](https://nodejs.org/api/querystring.html#querystring_que
251252
| Name | Type | Description | |
252253
| ---- | ---- | ----------- | -------- |
253254
| urlString | `string` | The URL string to parse. | &nbsp; |
254-
| parseQueryString&#x3D;false | `boolean` | If true, the query property will always be set to an object returned by the querystring module&#x27;s parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. | *Optional* |
255-
| slashesDenoteHost&#x3D;false | `boolean` | If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: &#x27;foo&#x27;, pathname: &#x27;/bar&#x27;} rather than {pathname: &#x27;//foo/bar&#x27;}. | *Optional* |
255+
| parseQueryString&#x3D;false | `boolean` | The query property will be set to an object returned by the querystring module&#x27;s parse() method if &#x60;true&#x60;. | *Optional* |
256+
| slashesDenoteHost&#x3D;false | `boolean` | The first token after the literal string // and preceding the next / will be interpreted as the host if &#x60;true&#x60;. | *Optional* |
256257

257258

258259
##### Examples
259260
```javascript
260-
fromURL('foo=1&foo=2&foo=3');
261-
// => { foo: ['1', '2', '3' ] }
262-
263-
fromURL('foo:1|foo:2|foo:3', '|', ':');
264-
// => { foo: ['1', '2', '3' ] }
261+
fromURL('https://www.dgmlive.com/kingcrimson/?album=discipline#track-1');
262+
// =>
263+
// {
264+
// protocol: 'https:',
265+
// slashes: true,
266+
// auth: null,
267+
// host: 'www.dgmlive.com',
268+
// port: null,
269+
// hostname: 'www.dgmlive.com',
270+
// hash: '#track-1',
271+
// search: '?album=discipline',
272+
// query: 'album=discipline',
273+
// pathname: '/kingcrimson/',
274+
// path: '/kingcrimson/?album=discipline',
275+
// href: 'https://www.dgmlive.com/kingcrimson/?album=discipline#track-1'
276+
// }
265277
```
266278

267279
##### Returns
268280
- `Object` Returns the parsed URL into a collection of key and value pairs.
269-
- `TypeError` A TypeError is thrown if urlString is not a string.
270-
- `URIError` A URIError is thrown if the auth property is present but cannot be decoded.
271281

272282
<a id="hasAnsi"></a>
273283
## hasAnsi(input)
@@ -695,10 +705,10 @@ From [querystring/stringify](https://nodejs.org/api/querystring.html#querystring
695705
| Name | Type | Description | |
696706
| ---- | ---- | ----------- | -------- |
697707
| str | `string` | The URL query string to parse. | &nbsp; |
698-
| sep&#x3D;&#x27;&amp;&#x27; | `string` | The substring used to delimit key and value pairs in the query string. Defaults to &#x27;&amp;&#x27;. | *Optional* |
699-
| eq&#x3D;&#x27;&#x3D;&#x27; | `string` | The substring used to delimit keys and values in the query string. Defaults to &#x27;&#x3D;&#x27;. | *Optional* |
708+
| sep&#x3D;&#x27;&amp;&#x27; | `string` | The substring to delimit key and value pairs. | *Optional* |
709+
| eq&#x3D;&#x27;&#x3D;&#x27; | `string` | The substring to delimit keys and values. | *Optional* |
700710
| options | `Object` | | *Optional* |
701-
| options.decodeURIComponent&#x3D;querystring.unescape() | `Function` | The function to use when decoding percent-encoded characters in the query string. | *Optional* |
711+
| options.decodeURIComponent&#x3D;querystring.unescape() | `Function` | The decoding function. | *Optional* |
702712

703713

704714
##### Examples
@@ -767,6 +777,36 @@ toUpper('abc');
767777
##### Returns
768778
- `string` Returns the upper case version of `str`.
769779

780+
<a id="toURL"></a>
781+
## toURL(urlObject)
782+
Convert an url object to URL.
783+
From [url/format](https://nodejs.org/api/url.html#url_url_format_urlobject)
784+
785+
786+
787+
788+
##### Parameters
789+
790+
| Name | Type | Description | |
791+
| ---- | ---- | ----------- | -------- |
792+
| urlObject | `Object` `string` | A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse(). | &nbsp; |
793+
794+
795+
##### Examples
796+
```javascript
797+
toURL({
798+
protocol: 'https',
799+
host: 'www.dgmlive.com',
800+
hash: '#track-1',
801+
query: { album: 'discipline' },
802+
pathname: '/kingcrimson'
803+
})
804+
// => "https://www.dgmlive.com/kingcrimson?album=discipline#track-1"
805+
```
806+
807+
##### Returns
808+
- `string` Returns the resulted URL.
809+
770810
<a id="trim"></a>
771811
## trim(str)
772812
Removes (strips) whitespace from both ends of the string.

fromQuery.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* Parse a query string into an object. Leading ? or # are ignored, so you can pass location.search or location.hash directly.
2+
* Parse a query string into an object.
33
* From [querystring/parse](https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options)
44
* @static
55
* @param {string} str The URL query string to parse.
6-
* @param {string} [sep='&'] The substring used to delimit key and value pairs in the query string. Defaults to '&'.
7-
* @param {string} [eq='='] The substring used to delimit keys and values in the query string. Defaults to '='.
6+
* @param {string} [sep='&'] The substring to delimit key and value pairs.
7+
* @param {string} [eq='='] The substring to delimit keys and values.
88
* @param {Object} [options]
9-
* @param {Function} [options.decodeURIComponent=querystring.unescape()] The function to use when decoding percent-encoded characters in the query string.
10-
* @param {Function} [options.maxKeys=1000] Specifies the maximum number of keys to parse. Defaults to 1000. Specify 0 to remove key counting limitations.
9+
* @param {Function} [options.decodeURIComponent=querystring.unescape()] The decoding function.
10+
* @param {Function} [options.maxKeys=1000] Maximum number of keys to parse. 0 to remove key counting limitations.
1111
* @returns {Object} Returns the parsed URL query string (str) into a collection of key and value pairs.
1212
* @example
1313
* fromQuery('foo=1&foo=2&foo=3');

fromURL.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Parse a query string into an object. Leading ? or # are ignored, so you can pass location.search or location.hash directly.
3+
* From [url/parse](https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost)
4+
* @static
5+
* @param {string} urlString The URL string to parse.
6+
* @param {boolean} [parseQueryString=false] The query property will be set to an object returned by the querystring module's parse() method if `true`.
7+
* @param {boolean} [slashesDenoteHost=false] The first token after the literal string // and preceding the next / will be interpreted as the host if `true`.
8+
* @returns {Object} Returns the parsed URL into a collection of key and value pairs.
9+
* @example
10+
* fromURL('https://www.dgmlive.com/kingcrimson/?album=discipline#track-1');
11+
* // =>
12+
* // {
13+
* // protocol: 'https:',
14+
* // slashes: true,
15+
* // auth: null,
16+
* // host: 'www.dgmlive.com',
17+
* // port: null,
18+
* // hostname: 'www.dgmlive.com',
19+
* // hash: '#track-1',
20+
* // search: '?album=discipline',
21+
* // query: 'album=discipline',
22+
* // pathname: '/kingcrimson/',
23+
* // path: '/kingcrimson/?album=discipline',
24+
* // href: 'https://www.dgmlive.com/kingcrimson/?album=discipline#track-1'
25+
* // }
26+
*/
27+
const fromURL = require('url').parse
28+
module.exports = fromURL

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ module.exports.slugify = require('./slugify')
2828
module.exports.toQuery = require('./toQuery')
2929
module.exports.fromQuery = require('./fromQuery')
3030
module.exports.join = require('./join')
31+
module.exports.fromURL = require('./fromURL')
32+
module.exports.toURL = require('./toURL')

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mellotron",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "Synthetic string orchestra. Curated list of string manipulation curried functions",
55
"homepage": "https://www.npmjs.com/package/mellotron",
66
"main": "dist/mellotron.js",
@@ -20,14 +20,15 @@
2020
"astral-regex": "1.0.0",
2121
"capitalize": "1.0.0",
2222
"chars": "2.2.0",
23-
"has-ansi": "^3.0.0",
23+
"has-ansi": "3.0.0",
2424
"lodash": "4.17.4",
2525
"querystring": "0.2.0",
2626
"ramda": "0.23.0",
2727
"slugify": "1.2.7",
2828
"string-format": "0.5.0",
2929
"string-length": "2.0.0",
30-
"strip-ansi": "4.0.0"
30+
"strip-ansi": "4.0.0",
31+
"url": "0.11.0"
3132
},
3233
"devDependencies": {
3334
"babel-cli": "6.26.0",

template.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
npm install {{title}}
99
```
1010

11+
## Methods
1112
{{#each files}}
1213
{{#each methods}}
1314
> [{{name}}](#{{name}})
1415
{{/each}}
1516
{{/each}}
1617

17-
1818
{{#each files}}
1919
{{#each methods}}
2020
<a id="{{name}}"></a>

toQuery.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* From [querystring/stringify](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options)
44
* @static
55
* @param {string} str The URL query string to parse.
6-
* @param {string} [sep='&'] The substring used to delimit key and value pairs in the query string. Defaults to '&'.
7-
* @param {string} [eq='='] The substring used to delimit keys and values in the query string. Defaults to '='.
6+
* @param {string} [sep='&'] The substring to delimit key and value pairs.
7+
* @param {string} [eq='='] The substring to delimit keys and values.
88
* @param {Object} [options]
9-
* @param {Function} [options.decodeURIComponent=querystring.unescape()] The function to use when decoding percent-encoded characters in the query string.
9+
* @param {Function} [options.decodeURIComponent=querystring.unescape()] The decoding function.
1010
* @returns {Object} Returns the parsed URL query string (str) into a collection of key and value pairs.
1111
* @example
1212
* toQuery({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });

toURL.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Convert an url object to URL.
3+
* From [url/format](https://nodejs.org/api/url.html#url_url_format_urlobject)
4+
* @static
5+
* @param {Object|string} urlObject A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse().
6+
* @returns {string} Returns the resulted URL.
7+
* @example
8+
* toURL({
9+
* protocol: 'https',
10+
* host: 'www.dgmlive.com',
11+
* hash: '#track-1',
12+
* query: { album: 'discipline' },
13+
* pathname: '/kingcrimson'
14+
* })
15+
* // => "https://www.dgmlive.com/kingcrimson?album=discipline#track-1"
16+
*/
17+
const toURL = require('url').format
18+
module.exports = toURL

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3485,7 +3485,7 @@ url-parse-lax@^1.0.0:
34853485
dependencies:
34863486
prepend-http "^1.0.1"
34873487

3488-
url@^0.11.0:
3488+
url@0.11.0, url@^0.11.0:
34893489
version "0.11.0"
34903490
resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1"
34913491
dependencies:

0 commit comments

Comments
 (0)