Skip to content

Commit 0ecb7af

Browse files
committed
Export as es6 module as well as cjs, v2
1 parent 2ef5bb4 commit 0ecb7af

File tree

6 files changed

+113
-15
lines changed

6 files changed

+113
-15
lines changed

build.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
// Setup
4+
const fs = require('fs')
5+
const pkg = require('./package.json')
6+
const semver = require('semver')
7+
const rollup = require('rollup').rollup
8+
const rollBabel = require('rollup-plugin-babel')
9+
const entry = 'tiny-params.js'
10+
const dest = 'index.js'
11+
const destEs6 = 'module.js'
12+
const git = require('simple-git')(__dirname)
13+
const babelOpts = {
14+
presets: [['es2015', {modules: false}]],
15+
plugins: ['external-helpers'],
16+
babelrc: false
17+
}
18+
const plugins = [rollBabel(babelOpts)]
19+
const toAdd = ['package.json', dest, destEs6]
20+
21+
// Main
22+
pkg.version = semver.inc(pkg.version, 'patch')
23+
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2), 'utf8')
24+
let cjsRollup = () => rollup({entry})
25+
let cjsBundle = (b) => b.write({format: 'cjs', dest})
26+
let rollCjs = () => cjsRollup().then(cjsBundle)
27+
let es6Rollup = () => rollup({entry, plugins})
28+
let es6Bundle = (b) => b.write({format: 'es', dest: destEs6})
29+
let rollEs6 = () => es6Rollup().then(es6Bundle)
30+
let gitAdd = () => new Promise((resolve, reject) => {
31+
git.add(toAdd, (err) => err ? reject(err) : resolve())
32+
})
33+
34+
rollCjs().then(rollEs6).then(gitAdd).catch(console.error)

index.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
'use strict'
1+
'use strict';
22

33
// export
4-
module.exports = (url) => {
4+
var tinyParams = (url) => {
55
if (!url || url === '' || !/\?/.test(url)) return {}
6-
var q = url.split(/\?(.+)?/)[1] || ''
7-
var obj = {}
8-
var ary = q.split('&')
6+
let q = url.split(/\?(.+)?/)[1] || '';
7+
let obj = {};
8+
let ary = q.split('&');
99
ary.forEach((q) => {
10-
q = (q.split('=') || [q]).map(decodeURIComponent)
11-
if (q[0] !== (q[0] = q[0].replace(/\[]$/, ''))) obj[q[0]] = obj[q[0]] || []
10+
q = (q.split('=') || [q]).map(decodeURIComponent);
11+
if (q[0] !== (q[0] = q[0].replace(/\[]$/, ''))) obj[q[0]] = obj[q[0]] || [];
1212
if (!obj[q[0]]) return (obj[q[0]] = q[1])
13-
if (Array.isArray(obj[q[0]])) obj[q[0]] = obj[q[0]].concat([q[1]])
14-
else obj[q[0]] = [obj[q[0]]].concat([q[1]])
15-
})
13+
if (Array.isArray(obj[q[0]])) obj[q[0]] = obj[q[0]].concat([q[1]]);
14+
else obj[q[0]] = [obj[q[0]]].concat([q[1]]);
15+
});
1616
return obj
17-
}
17+
};
18+
19+
module.exports = tinyParams;

module.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// export
2+
3+
var tinyParams = (function (url) {
4+
if (!url || url === '' || !/\?/.test(url)) return {};
5+
var q = url.split(/\?(.+)?/)[1] || '';
6+
var obj = {};
7+
var ary = q.split('&');
8+
ary.forEach(function (q) {
9+
q = (q.split('=') || [q]).map(decodeURIComponent);
10+
if (q[0] !== (q[0] = q[0].replace(/\[]$/, ''))) obj[q[0]] = obj[q[0]] || [];
11+
if (!obj[q[0]]) return obj[q[0]] = q[1];
12+
if (Array.isArray(obj[q[0]])) obj[q[0]] = obj[q[0]].concat([q[1]]);else obj[q[0]] = [obj[q[0]]].concat([q[1]]);
13+
});
14+
return obj;
15+
});
16+
17+
export default tinyParams;

package.json

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"name": "tiny-params",
3-
"version": "1.0.3",
3+
"version": "2.0.2",
44
"description": "A tiny URL param parser, suitable for server or browser",
55
"main": "index.js",
6-
"files": ["index.js"],
6+
"module": "module.js",
7+
"files": [
8+
"index.js",
9+
"module.js"
10+
],
711
"scripts": {
812
"test": "ava --verbose"
913
},
@@ -28,6 +32,20 @@
2832
},
2933
"homepage": "https://github.com/doesdev/tiny-params#readme",
3034
"devDependencies": {
31-
"ava": "^0.18.2"
35+
"ava": "^0.19.1",
36+
"babel-plugin-external-helpers": "^6.22.0",
37+
"babel-polyfill": "^6.23.0",
38+
"babel-preset-es2015": "^6.24.1",
39+
"ghooks": "^2.0.0",
40+
"rollup": "^0.41.6",
41+
"rollup-plugin-babel": "^2.7.1",
42+
"semver": "^5.3.0",
43+
"simple-git": "^1.72.0"
44+
},
45+
"config": {
46+
"ghooks": {
47+
"pre-commit": "node build",
48+
"post-commit": "node post-commit"
49+
}
3250
}
33-
}
51+
}

post-commit.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const pkg = require('./package.json')
4+
const git = require('simple-git')(__dirname)
5+
git.addTag(pkg.version, (err) => {
6+
if (err) return console.error(err)
7+
git.pushTags('origin', (err) => {
8+
if (err) return console.error(err)
9+
})
10+
})

tiny-params.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
// export
4+
export default (url) => {
5+
if (!url || url === '' || !/\?/.test(url)) return {}
6+
let q = url.split(/\?(.+)?/)[1] || ''
7+
let obj = {}
8+
let ary = q.split('&')
9+
ary.forEach((q) => {
10+
q = (q.split('=') || [q]).map(decodeURIComponent)
11+
if (q[0] !== (q[0] = q[0].replace(/\[]$/, ''))) obj[q[0]] = obj[q[0]] || []
12+
if (!obj[q[0]]) return (obj[q[0]] = q[1])
13+
if (Array.isArray(obj[q[0]])) obj[q[0]] = obj[q[0]].concat([q[1]])
14+
else obj[q[0]] = [obj[q[0]]].concat([q[1]])
15+
})
16+
return obj
17+
}

0 commit comments

Comments
 (0)