Dev: Complete test and switch phantomjs to chrome headless
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "8"
|
||||
addons:
|
||||
chrome: stable
|
||||
before_script:
|
||||
- npm install -g codecov
|
||||
script:
|
||||
|
||||
@@ -13,6 +13,7 @@ module.exports = function (config)
|
||||
'node_modules/karma-jasmine/lib/boot.js',
|
||||
'node_modules/karma-jasmine/lib/adapter.js',
|
||||
'node_modules/jasmine-jquery/lib/jasmine-jquery.js',
|
||||
'test/util.js',
|
||||
'test/console.js',
|
||||
'test/elements.js',
|
||||
'test/features.js',
|
||||
@@ -27,8 +28,7 @@ module.exports = function (config)
|
||||
plugins: [
|
||||
'karma-jasmine',
|
||||
'karma-jquery',
|
||||
'karma-phantomjs-launcher',
|
||||
'karma-wrap-preprocessor',
|
||||
'karma-chrome-launcher',
|
||||
'karma-coverage',
|
||||
'karma-webpack',
|
||||
'karma-sourcemap-loader',
|
||||
@@ -38,13 +38,9 @@ module.exports = function (config)
|
||||
noInfo: true
|
||||
},
|
||||
preprocessors: {
|
||||
'test/*.js': ['wrap'],
|
||||
'src/index.js': ['webpack', 'sourcemap', 'sourcemap-writer', 'coverage']
|
||||
},
|
||||
webpack: webpackCfg,
|
||||
wrapPreprocessor: {
|
||||
template: '(function () { <%= contents %> })()'
|
||||
},
|
||||
coverageReporter: {
|
||||
type: 'json',
|
||||
subdir: '.',
|
||||
@@ -54,8 +50,8 @@ module.exports = function (config)
|
||||
port: 9876,
|
||||
colors: true,
|
||||
logLevel: config.LOG_INFO,
|
||||
autoWatch: true,
|
||||
browsers: ['PhantomJS'],
|
||||
autoWatch: false,
|
||||
browsers: ['ChromeHeadless'],
|
||||
singleRun: true,
|
||||
concurrency: Infinity
|
||||
});
|
||||
|
||||
214
package-lock.json
generated
214
package-lock.json
generated
@@ -2796,12 +2796,6 @@
|
||||
"event-emitter": "0.3.5"
|
||||
}
|
||||
},
|
||||
"es6-promise": {
|
||||
"version": "4.0.5",
|
||||
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.0.5.tgz",
|
||||
"integrity": "sha1-eILzCt3lskDM+n99eMVIMwlRrkI=",
|
||||
"dev": true
|
||||
},
|
||||
"es6-set": {
|
||||
"version": "0.1.5",
|
||||
"resolved": "https://registry.npmjs.org/es6-set/-/es6-set-0.1.5.tgz",
|
||||
@@ -3287,44 +3281,6 @@
|
||||
"is-extglob": "1.0.0"
|
||||
}
|
||||
},
|
||||
"extract-zip": {
|
||||
"version": "1.6.5",
|
||||
"resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-1.6.5.tgz",
|
||||
"integrity": "sha1-maBnNbbqIOqbcF13ms/8yHz/BEA=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"concat-stream": "1.6.0",
|
||||
"debug": "2.2.0",
|
||||
"mkdirp": "0.5.0",
|
||||
"yauzl": "2.4.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz",
|
||||
"integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"ms": "0.7.1"
|
||||
}
|
||||
},
|
||||
"mkdirp": {
|
||||
"version": "0.5.0",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.0.tgz",
|
||||
"integrity": "sha1-HXMHam35hs2TROFecfzAWkyavxI=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"minimist": "0.0.8"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "0.7.1",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz",
|
||||
"integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"extsprintf": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz",
|
||||
@@ -3401,15 +3357,6 @@
|
||||
"websocket-driver": "0.7.0"
|
||||
}
|
||||
},
|
||||
"fd-slicer": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.0.1.tgz",
|
||||
"integrity": "sha1-i1vL2ewyfFBBv5qwI/1nUPEXfmU=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"pend": "1.2.0"
|
||||
}
|
||||
},
|
||||
"figures": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/figures/-/figures-2.0.0.tgz",
|
||||
@@ -3595,23 +3542,21 @@
|
||||
"integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=",
|
||||
"dev": true
|
||||
},
|
||||
"fs-access": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/fs-access/-/fs-access-1.0.1.tgz",
|
||||
"integrity": "sha1-1qh/JiJxzv6+wwxVNAf7mV2od3o=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"null-check": "1.0.0"
|
||||
}
|
||||
},
|
||||
"fs-exists-sync": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/fs-exists-sync/-/fs-exists-sync-0.1.0.tgz",
|
||||
"integrity": "sha1-mC1ok6+RjnLQjeyehnP/K1qNat0=",
|
||||
"dev": true
|
||||
},
|
||||
"fs-extra": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-1.0.0.tgz",
|
||||
"integrity": "sha1-zTzl9+fLYUWIP8rjGR6Yd/hYeVA=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"graceful-fs": "4.1.11",
|
||||
"jsonfile": "2.4.0",
|
||||
"klaw": "1.3.1"
|
||||
}
|
||||
},
|
||||
"fs.realpath": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
|
||||
@@ -4094,16 +4039,6 @@
|
||||
"minimalistic-assert": "1.0.0"
|
||||
}
|
||||
},
|
||||
"hasha": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/hasha/-/hasha-2.2.0.tgz",
|
||||
"integrity": "sha1-eNfL/B5tZjA/55g3NlmEUXsvbuE=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"is-stream": "1.1.0",
|
||||
"pinkie-promise": "2.0.1"
|
||||
}
|
||||
},
|
||||
"hawk": {
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/hawk/-/hawk-3.1.3.tgz",
|
||||
@@ -4970,15 +4905,6 @@
|
||||
"integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=",
|
||||
"dev": true
|
||||
},
|
||||
"jsonfile": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-2.4.0.tgz",
|
||||
"integrity": "sha1-NzaitCi4e72gzIO1P6PWM6NcKug=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"graceful-fs": "4.1.11"
|
||||
}
|
||||
},
|
||||
"jsonify": {
|
||||
"version": "0.0.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz",
|
||||
@@ -5063,6 +4989,16 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"karma-chrome-launcher": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/karma-chrome-launcher/-/karma-chrome-launcher-2.2.0.tgz",
|
||||
"integrity": "sha512-uf/ZVpAabDBPvdPdveyk1EPgbnloPvFFGgmRhYLTDH7gEB4nZdSBk8yTU47w1g/drLSx5uMOkjKk7IWKfWg/+w==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"fs-access": "1.0.1",
|
||||
"which": "1.3.0"
|
||||
}
|
||||
},
|
||||
"karma-coverage": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/karma-coverage/-/karma-coverage-1.1.1.tgz",
|
||||
@@ -5102,16 +5038,6 @@
|
||||
"integrity": "sha1-a+6TRU2LCWvMeBVXCoHnP93bXW8=",
|
||||
"dev": true
|
||||
},
|
||||
"karma-phantomjs-launcher": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/karma-phantomjs-launcher/-/karma-phantomjs-launcher-1.0.4.tgz",
|
||||
"integrity": "sha1-0jyjSAG9qYY60xjju0vUBisTrNI=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"lodash": "4.17.4",
|
||||
"phantomjs-prebuilt": "2.1.15"
|
||||
}
|
||||
},
|
||||
"karma-sourcemap-loader": {
|
||||
"version": "0.3.7",
|
||||
"resolved": "https://registry.npmjs.org/karma-sourcemap-loader/-/karma-sourcemap-loader-0.3.7.tgz",
|
||||
@@ -5178,44 +5104,12 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"karma-wrap-preprocessor": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/karma-wrap-preprocessor/-/karma-wrap-preprocessor-0.1.0.tgz",
|
||||
"integrity": "sha1-FTCYal1RN0XNhe9Rji5pJnuaoBc=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"lodash": "2.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash": {
|
||||
"version": "2.4.2",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-2.4.2.tgz",
|
||||
"integrity": "sha1-+t2DS5aDBz2hebPq5tnA0VBT9z4=",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"kew": {
|
||||
"version": "0.7.0",
|
||||
"resolved": "https://registry.npmjs.org/kew/-/kew-0.7.0.tgz",
|
||||
"integrity": "sha1-edk9LTM2PW/dKXCzNdkUGtWR15s=",
|
||||
"dev": true
|
||||
},
|
||||
"kind-of": {
|
||||
"version": "5.1.0",
|
||||
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-5.1.0.tgz",
|
||||
"integrity": "sha512-NGEErnH6F2vUuXDh+OlbcKW7/wOcfdRHaZ7VWtqCztfHri/++YKmP51OdWeGPuqCOba6kk2OTe5d02VmTB80Pw==",
|
||||
"dev": true
|
||||
},
|
||||
"klaw": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/klaw/-/klaw-1.3.1.tgz",
|
||||
"integrity": "sha1-QIhDO0azsbolnXh4XY6W9zugJDk=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"graceful-fs": "4.1.11"
|
||||
}
|
||||
},
|
||||
"lazy-cache": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/lazy-cache/-/lazy-cache-2.0.2.tgz",
|
||||
@@ -6143,6 +6037,12 @@
|
||||
"set-blocking": "2.0.0"
|
||||
}
|
||||
},
|
||||
"null-check": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/null-check/-/null-check-1.0.0.tgz",
|
||||
"integrity": "sha1-l33/1xdgErnsMNKjnbXPcqBDnt0=",
|
||||
"dev": true
|
||||
},
|
||||
"num2fraction": {
|
||||
"version": "1.2.2",
|
||||
"resolved": "https://registry.npmjs.org/num2fraction/-/num2fraction-1.2.2.tgz",
|
||||
@@ -6499,52 +6399,12 @@
|
||||
"sha.js": "2.4.9"
|
||||
}
|
||||
},
|
||||
"pend": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz",
|
||||
"integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=",
|
||||
"dev": true
|
||||
},
|
||||
"performance-now": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-0.2.0.tgz",
|
||||
"integrity": "sha1-M+8wxcd9TqIcWlOGnZG1bY8lVeU=",
|
||||
"dev": true
|
||||
},
|
||||
"phantomjs-prebuilt": {
|
||||
"version": "2.1.15",
|
||||
"resolved": "https://registry.npmjs.org/phantomjs-prebuilt/-/phantomjs-prebuilt-2.1.15.tgz",
|
||||
"integrity": "sha1-IPhugtM0nFBZF1J3RbekEeCLOQM=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"es6-promise": "4.0.5",
|
||||
"extract-zip": "1.6.5",
|
||||
"fs-extra": "1.0.0",
|
||||
"hasha": "2.2.0",
|
||||
"kew": "0.7.0",
|
||||
"progress": "1.1.8",
|
||||
"request": "2.81.0",
|
||||
"request-progress": "2.0.1",
|
||||
"which": "1.2.14"
|
||||
},
|
||||
"dependencies": {
|
||||
"progress": {
|
||||
"version": "1.1.8",
|
||||
"resolved": "https://registry.npmjs.org/progress/-/progress-1.1.8.tgz",
|
||||
"integrity": "sha1-4mDHj2Fhzdmw5WzD4Khd4Xx6V74=",
|
||||
"dev": true
|
||||
},
|
||||
"which": {
|
||||
"version": "1.2.14",
|
||||
"resolved": "https://registry.npmjs.org/which/-/which-1.2.14.tgz",
|
||||
"integrity": "sha1-mofEN48D6CfOyvGs31bHNsAcFOU=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"isexe": "2.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"pify": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz",
|
||||
@@ -9124,15 +8984,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"request-progress": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/request-progress/-/request-progress-2.0.1.tgz",
|
||||
"integrity": "sha1-XTa7V5YcZzqlt4jbyBQf3yO0Tgg=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"throttleit": "1.0.0"
|
||||
}
|
||||
},
|
||||
"require-directory": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
|
||||
@@ -10122,12 +9973,6 @@
|
||||
"integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=",
|
||||
"dev": true
|
||||
},
|
||||
"throttleit": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/throttleit/-/throttleit-1.0.0.tgz",
|
||||
"integrity": "sha1-nnhYNtr0Z0MUWlmEtiaNgoUorGw=",
|
||||
"dev": true
|
||||
},
|
||||
"through": {
|
||||
"version": "2.3.8",
|
||||
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
||||
@@ -11197,15 +11042,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"yauzl": {
|
||||
"version": "2.4.1",
|
||||
"resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.4.1.tgz",
|
||||
"integrity": "sha1-lSj0QtqxsihOWLQ3m7GU4i4MQAU=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"fd-slicer": "1.0.1"
|
||||
}
|
||||
},
|
||||
"yeast": {
|
||||
"version": "0.1.2",
|
||||
"resolved": "https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz",
|
||||
|
||||
@@ -68,14 +68,13 @@
|
||||
"js-beautify": "^1.7.4",
|
||||
"json-loader": "^0.5.7",
|
||||
"karma": "^1.7.1",
|
||||
"karma-chrome-launcher": "^2.2.0",
|
||||
"karma-coverage": "^1.1.1",
|
||||
"karma-jasmine": "^1.1.0",
|
||||
"karma-jquery": "^0.2.2",
|
||||
"karma-phantomjs-launcher": "^1.0.4",
|
||||
"karma-sourcemap-loader": "^0.3.7",
|
||||
"karma-sourcemap-writer": "^0.1.2",
|
||||
"karma-webpack": "^2.0.5",
|
||||
"karma-wrap-preprocessor": "^0.1.0",
|
||||
"modernizr": "^3.5.0",
|
||||
"node-sass": "^4.5.3",
|
||||
"postcss-class-prefix": "^0.3.0",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<div class="eruda-section">
|
||||
<div class="eruda-section eruda-local-storage">
|
||||
<h2 class="eruda-title">
|
||||
Local Storage
|
||||
<div class="eruda-btn eruda-refresh-local-storage">
|
||||
@@ -28,7 +28,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="eruda-section">
|
||||
<div class="eruda-section eruda-session-storage">
|
||||
<h2 class="eruda-title">
|
||||
Session Storage
|
||||
<div class="eruda-btn eruda-refresh-session-storage">
|
||||
@@ -58,7 +58,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="eruda-section">
|
||||
<div class="eruda-section eruda-cookie">
|
||||
<h2 class="eruda-title {{cookieState}}">
|
||||
Cookie
|
||||
<div class="eruda-btn eruda-refresh-cookie">
|
||||
@@ -88,7 +88,7 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="eruda-section">
|
||||
<div class="eruda-section eruda-script">
|
||||
<h2 class="eruda-title {{scriptState}}">
|
||||
Script
|
||||
<div class="eruda-btn eruda-refresh-script">
|
||||
@@ -107,7 +107,7 @@
|
||||
{{/if}}
|
||||
</ul>
|
||||
</div>
|
||||
<div class="eruda-section">
|
||||
<div class="eruda-section eruda-stylesheet">
|
||||
<h2 class="eruda-title {{stylesheetState}}">
|
||||
Stylesheet
|
||||
<div class="eruda-btn eruda-refresh-stylesheet">
|
||||
@@ -126,7 +126,7 @@
|
||||
{{/if}}
|
||||
</ul>
|
||||
</div>
|
||||
<div class="eruda-section">
|
||||
<div class="eruda-section eruda-image">
|
||||
<h2 class="eruda-title {{imageState}}">
|
||||
Image
|
||||
<div class="eruda-btn eruda-refresh-image">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
describe('console', function ()
|
||||
{
|
||||
var tool = eruda.get('console'),
|
||||
$tool = $('.eruda-console');
|
||||
$tool = $('.eruda-console');
|
||||
|
||||
beforeEach(function ()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
describe('features', function ()
|
||||
{
|
||||
var tool = eruda.get('features'),
|
||||
$tool = $('.eruda-features');
|
||||
|
||||
beforeEach(function ()
|
||||
{
|
||||
eruda.show('features');
|
||||
});
|
||||
|
||||
it('items', function (done)
|
||||
{
|
||||
setTimeout(function ()
|
||||
{
|
||||
expect($tool.find('li')).toHaveLength(45);
|
||||
done();
|
||||
}, 1000);
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,7 @@
|
||||
<script src="lib/jasmine-html.js"></script>
|
||||
<script src="assets/eruda.js"></script>
|
||||
<script src="boot.js"></script>
|
||||
<script src="util.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script>boot('network');</script>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
describe('network', function ()
|
||||
{
|
||||
var tool = eruda.get('network'),
|
||||
$tool = $('.eruda-network');
|
||||
|
||||
beforeEach(function ()
|
||||
{
|
||||
eruda.show('network');
|
||||
});
|
||||
|
||||
describe('request', function ()
|
||||
{
|
||||
it('xhr', function (done)
|
||||
{
|
||||
$('.eruda-clear-xhr').click();
|
||||
_.ajax.get(window.location.toString(), function ()
|
||||
{
|
||||
expect($('.eruda-requests li')).toHaveLength(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,7 @@
|
||||
<script src="lib/jasmine-html.js"></script>
|
||||
<script src="assets/eruda.js"></script>
|
||||
<script src="boot.js"></script>
|
||||
<script src="util.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script>boot('resources');</script>
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
describe('resources', function ()
|
||||
{
|
||||
var tool = eruda.get('resources'),
|
||||
$tool = $('.eruda-resources');
|
||||
|
||||
beforeEach(function ()
|
||||
{
|
||||
eruda.show('resources');
|
||||
});
|
||||
|
||||
describe('localStorage', function ()
|
||||
{
|
||||
it('show', function ()
|
||||
{
|
||||
localStorage.clear();
|
||||
localStorage.setItem('testKey', 'testVal');
|
||||
$tool.find('.eruda-refresh-local-storage').click();
|
||||
expect($tool.find('.eruda-local-storage')).toContainText('testKey');
|
||||
});
|
||||
|
||||
it('clear', function ()
|
||||
{
|
||||
$tool.find('.eruda-clear-storage[data-type="local"]').click();
|
||||
expect($tool.find('.eruda-local-storage')).toContainText('Empty');
|
||||
});
|
||||
});
|
||||
|
||||
describe('sessionStorage', function ()
|
||||
{
|
||||
it('show', function ()
|
||||
{
|
||||
sessionStorage.clear();
|
||||
sessionStorage.setItem('testKey', 'testVal');
|
||||
$tool.find('.eruda-refresh-session-storage').click();
|
||||
expect($tool.find('.eruda-session-storage')).toContainText('testKey');
|
||||
});
|
||||
|
||||
it('clear', function ()
|
||||
{
|
||||
$tool.find('.eruda-clear-storage[data-type="session"]').click();
|
||||
expect($tool.find('.eruda-session-storage')).toContainText('Empty');
|
||||
});
|
||||
});
|
||||
|
||||
describe('cookie', function ()
|
||||
{
|
||||
it('show', function ()
|
||||
{
|
||||
_.cookie.set('testKey', 'testVal');
|
||||
$tool.find('.eruda-refresh-cookie').click();
|
||||
expect($tool.find('.eruda-cookie')).toContainText('testKey');
|
||||
});
|
||||
|
||||
it('clear', function ()
|
||||
{
|
||||
$tool.find('.eruda-clear-cookie').click();
|
||||
expect($tool.find('.eruda-cookie')).toContainText('Empty');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -8,6 +8,14 @@ describe('snippets', function ()
|
||||
it('border all', function ()
|
||||
{
|
||||
expect($tool.find('.eruda-name').eq(0)).toContainText('Border All');
|
||||
|
||||
var $body = $('body'),
|
||||
$btn = $tool.find('.eruda-run').eq(0);
|
||||
|
||||
$btn.click();
|
||||
expect($body).toHaveCss({outlineWidth: '2px'});
|
||||
$btn.click();
|
||||
expect($body).toHaveCss({outlineWidth: '0px'});
|
||||
});
|
||||
|
||||
it('refresh page', function ()
|
||||
@@ -23,31 +31,36 @@ describe('snippets', function ()
|
||||
it('edit page', function ()
|
||||
{
|
||||
expect($tool.find('.eruda-name').eq(3)).toContainText('Edit Page');
|
||||
|
||||
var $body = $('body'),
|
||||
$btn = $tool.find('.eruda-run').eq(3);
|
||||
|
||||
$btn.click();
|
||||
expect($body).toHaveAttr('contenteditable', 'true');
|
||||
$btn.click();
|
||||
expect($body).toHaveAttr('contenteditable', 'false');
|
||||
});
|
||||
});
|
||||
|
||||
describe('basic', function ()
|
||||
it('clear', function ()
|
||||
{
|
||||
it('clear', function ()
|
||||
{
|
||||
tool.clear();
|
||||
expect($tool.find('.eruda-name')).toHaveLength(0);
|
||||
});
|
||||
tool.clear();
|
||||
expect($tool.find('.eruda-name')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('add', function ()
|
||||
it('add', function ()
|
||||
{
|
||||
tool.add('Test', function ()
|
||||
{
|
||||
tool.add('Test', function ()
|
||||
{
|
||||
console.log('eruda');
|
||||
}, 'This is the description');
|
||||
expect($tool.find('.eruda-name')).toContainText('Test');
|
||||
expect($tool.find('.eruda-description')).toContainText('This is the description');
|
||||
});
|
||||
console.log('eruda');
|
||||
}, 'This is the description');
|
||||
expect($tool.find('.eruda-name')).toContainText('Test');
|
||||
expect($tool.find('.eruda-description')).toContainText('This is the description');
|
||||
});
|
||||
|
||||
it('remove', function ()
|
||||
{
|
||||
tool.remove('Test');
|
||||
expect($tool.find('.eruda-name')).toHaveLength(0);
|
||||
});
|
||||
it('remove', function ()
|
||||
{
|
||||
tool.remove('Test');
|
||||
expect($tool.find('.eruda-name')).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -3,12 +3,17 @@ describe('sources', function ()
|
||||
var tool = eruda.get('sources'),
|
||||
$tool = $('.eruda-sources');
|
||||
|
||||
describe('highlight code', function ()
|
||||
beforeEach(function ()
|
||||
{
|
||||
it('js', function ()
|
||||
eruda.show('sources');
|
||||
});
|
||||
|
||||
describe('js', function ()
|
||||
{
|
||||
it('highlight', function ()
|
||||
{
|
||||
tool.set('js', '/* test */');
|
||||
expect($tool.find('.eruda-content')).toContainHtml('<span style="color:#63a35c;">/* test */</span>');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
856
test/util.js
856
test/util.js
@@ -5,6 +5,98 @@ window._ = (function()
|
||||
|
||||
if (typeof window === 'object' && window._) _ = window._;
|
||||
|
||||
/* ------------------------------ noop ------------------------------ */
|
||||
|
||||
var noop = _.noop = (function ()
|
||||
{
|
||||
/* A no-operation function.
|
||||
*
|
||||
* ```javascript
|
||||
* noop(); // Does nothing
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
function exports() {}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ isObj ------------------------------ */
|
||||
|
||||
var isObj = _.isObj = (function ()
|
||||
{
|
||||
/* Check if value is the language type of Object.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|-------|--------------------------|
|
||||
* |val |* |Value to check |
|
||||
* |return|boolean|True if value is an object|
|
||||
*
|
||||
* [Language Spec](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
|
||||
*
|
||||
* ```javascript
|
||||
* isObj({}); // -> true
|
||||
* isObj([]); // -> true
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
function exports(val)
|
||||
{
|
||||
var type = typeof val;
|
||||
|
||||
return !!val && (type === 'function' || type === 'object');
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ allKeys ------------------------------ */
|
||||
|
||||
var allKeys = _.allKeys = (function ()
|
||||
{
|
||||
/* Retrieve all the names of object's own and inherited properties.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|------|---------------------------|
|
||||
* |obj |object|Object to query |
|
||||
* |return|array |Array of all property names|
|
||||
*
|
||||
* > Members of Object's prototype won't be retrieved.
|
||||
*
|
||||
* ```javascript
|
||||
* var obj = Object.create({zero: 0});
|
||||
* obj.one = 1;
|
||||
* allKeys(obj) // -> ['zero', 'one']
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
function exports(obj)
|
||||
{
|
||||
var ret = [], key;
|
||||
|
||||
for (key in obj) ret.push(key);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ isUndef ------------------------------ */
|
||||
|
||||
var isUndef = _.isUndef = (function ()
|
||||
@@ -244,6 +336,41 @@ window._ = (function()
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ isArgs ------------------------------ */
|
||||
|
||||
var isArgs = _.isArgs = (function ()
|
||||
{
|
||||
/* Check if value is classified as an arguments object.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|-------|------------------------------------|
|
||||
* |value |* |Value to check |
|
||||
* |return|boolean|True if value is an arguments object|
|
||||
*
|
||||
* ```javascript
|
||||
* (function () {
|
||||
* isArgs(arguments); // -> true
|
||||
* })();
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* objToStr
|
||||
*/
|
||||
|
||||
function exports(val)
|
||||
{
|
||||
return objToStr(val) === '[object Arguments]';
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ isFn ------------------------------ */
|
||||
|
||||
var isFn = _.isFn = (function ()
|
||||
@@ -455,6 +582,158 @@ window._ = (function()
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ defaults ------------------------------ */
|
||||
|
||||
var defaults = _.defaults = (function (exports)
|
||||
{
|
||||
/* Fill in undefined properties in object with the first value present in the following list of defaults objects.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|------|------------------|
|
||||
* |obj |object|Destination object|
|
||||
* |*src |object|Sources objects |
|
||||
* |return|object|Destination object|
|
||||
*
|
||||
* ```javascript
|
||||
* defaults({name: 'RedHood'}, {name: 'Unknown', age: 24}); // -> {name: 'RedHood', age: 24}
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* createAssigner allKeys
|
||||
*/
|
||||
|
||||
exports = createAssigner(allKeys, true);
|
||||
|
||||
return exports;
|
||||
})({});
|
||||
|
||||
/* ------------------------------ cookie ------------------------------ */
|
||||
|
||||
_.cookie = (function (exports)
|
||||
{
|
||||
/* Simple api for handling browser cookies.
|
||||
*
|
||||
* ### get
|
||||
*
|
||||
* Get cookie value.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|------|--------------------------|
|
||||
* |key |string|Cookie key |
|
||||
* |return|string|Corresponding cookie value|
|
||||
*
|
||||
* ### set
|
||||
*
|
||||
* Set cookie value.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |---------|-------|--------------|
|
||||
* |key |string |Cookie key |
|
||||
* |val |string |Cookie value |
|
||||
* |[options]|object |Cookie options|
|
||||
* |return |exports|Module cookie |
|
||||
*
|
||||
* ### remove
|
||||
*
|
||||
* Remove cookie value.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |---------|-------|--------------|
|
||||
* |key |string |Cookie key |
|
||||
* |[options]|object |Cookie options|
|
||||
* |return |exports|Module cookie |
|
||||
*
|
||||
* ```javascript
|
||||
* cookie.set('a', '1', {path: '/'});
|
||||
* cookie.get('a'); // -> '1'
|
||||
* cookie.remove('a');
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: browser
|
||||
* test: browser
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* defaults isNum isUndef
|
||||
*/
|
||||
|
||||
var defOpts = { path: '/' };
|
||||
|
||||
function setCookie(key, val, options)
|
||||
{
|
||||
if (!isUndef(val))
|
||||
{
|
||||
options = options || {};
|
||||
options = defaults(options, defOpts);
|
||||
|
||||
if (isNum(options.expires))
|
||||
{
|
||||
var expires = new Date();
|
||||
expires.setMilliseconds(expires.getMilliseconds() + options.expires * 864e+5);
|
||||
options.expires = expires;
|
||||
}
|
||||
|
||||
val = encodeURIComponent(val);
|
||||
key = encodeURIComponent(key);
|
||||
|
||||
document.cookie = [
|
||||
key, '=', val,
|
||||
options.expires && '; expires=' + options.expires.toUTCString(),
|
||||
options.path && '; path=' + options.path,
|
||||
options.domain && '; domain=' + options.domain,
|
||||
options.secure ? '; secure' : ''
|
||||
].join('');
|
||||
|
||||
return exports;
|
||||
}
|
||||
|
||||
var cookies = document.cookie ? document.cookie.split('; ') : [],
|
||||
result = key ? undefined : {};
|
||||
|
||||
for (var i = 0, len = cookies.length; i < len; i++)
|
||||
{
|
||||
var c = cookies[i],
|
||||
parts = c.split('='),
|
||||
name = decodeURIComponent(parts.shift());
|
||||
|
||||
c = parts.join('=');
|
||||
c = decodeURIComponent(c);
|
||||
|
||||
if (key === name)
|
||||
{
|
||||
result = c;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!key) result[name] = c;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
exports = {
|
||||
get: setCookie,
|
||||
set: setCookie,
|
||||
remove: function (key, options)
|
||||
{
|
||||
options = options || {};
|
||||
options.expires = -1;
|
||||
|
||||
return setCookie(key, '', options);
|
||||
}
|
||||
};
|
||||
|
||||
return exports;
|
||||
})({});
|
||||
|
||||
/* ------------------------------ extendOwn ------------------------------ */
|
||||
|
||||
var extendOwn = _.extendOwn = (function (exports)
|
||||
@@ -486,6 +765,115 @@ window._ = (function()
|
||||
return exports;
|
||||
})({});
|
||||
|
||||
/* ------------------------------ isArr ------------------------------ */
|
||||
|
||||
var isArr = _.isArr = (function (exports)
|
||||
{
|
||||
/* Check if value is an `Array` object.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|-------|----------------------------------|
|
||||
* |val |* |The value to check |
|
||||
* |return|boolean|True if value is an `Array` object|
|
||||
*
|
||||
* ```javascript
|
||||
* isArr([]); // -> true
|
||||
* isArr({}); // -> false
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* objToStr
|
||||
*/
|
||||
|
||||
exports = Array.isArray || function (val)
|
||||
{
|
||||
return objToStr(val) === '[object Array]';
|
||||
};
|
||||
|
||||
return exports;
|
||||
})({});
|
||||
|
||||
/* ------------------------------ isStr ------------------------------ */
|
||||
|
||||
var isStr = _.isStr = (function ()
|
||||
{
|
||||
/* Check if value is a string primitive.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|-------|-----------------------------------|
|
||||
* |val |* |Value to check |
|
||||
* |return|boolean|True if value is a string primitive|
|
||||
*
|
||||
* ```javascript
|
||||
* isStr('eris'); // -> true
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* objToStr
|
||||
*/
|
||||
|
||||
function exports(val)
|
||||
{
|
||||
return objToStr(val) === '[object String]';
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ isEmpty ------------------------------ */
|
||||
|
||||
var isEmpty = _.isEmpty = (function ()
|
||||
{
|
||||
/* Check if value is an empty object or array.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|-------|----------------------|
|
||||
* |val |* |Value to check |
|
||||
* |return|boolean|True if value is empty|
|
||||
*
|
||||
* ```javascript
|
||||
* isEmpty([]); // -> true
|
||||
* isEmpty({}); // -> true
|
||||
* isEmpty(''); // -> true
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* isArrLike isArr isStr isArgs keys
|
||||
*/
|
||||
|
||||
function exports(val)
|
||||
{
|
||||
if (val == null) return true;
|
||||
|
||||
if (isArrLike(val) && (isArr(val) || isStr(val) || isArgs(val)))
|
||||
{
|
||||
return val.length === 0;
|
||||
}
|
||||
|
||||
return keys(val).length === 0;
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ isMatch ------------------------------ */
|
||||
|
||||
var isMatch = _.isMatch = (function ()
|
||||
@@ -533,22 +921,22 @@ window._ = (function()
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ isObj ------------------------------ */
|
||||
/* ------------------------------ ltrim ------------------------------ */
|
||||
|
||||
var isObj = _.isObj = (function ()
|
||||
var ltrim = _.ltrim = (function ()
|
||||
{
|
||||
/* Check if value is the language type of Object.
|
||||
/* Remove chars or white-spaces from beginning of string.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|-------|--------------------------|
|
||||
* |val |* |Value to check |
|
||||
* |return|boolean|True if value is an object|
|
||||
*
|
||||
* [Language Spec](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
|
||||
* |Name |Type |Desc |
|
||||
* |------|------------|------------------|
|
||||
* |str |string |String to trim |
|
||||
* |chars |string array|Characters to trim|
|
||||
* |return|string |Trimmed string |
|
||||
*
|
||||
* ```javascript
|
||||
* isObj({}); // -> true
|
||||
* isObj([]); // -> true
|
||||
* ltrim(' abc '); // -> 'abc '
|
||||
* ltrim('_abc_', '_'); // -> 'abc_'
|
||||
* ltrim('_abc_', ['a', '_']); // -> 'bc_'
|
||||
* ```
|
||||
*/
|
||||
|
||||
@@ -557,11 +945,36 @@ window._ = (function()
|
||||
* test: all
|
||||
*/
|
||||
|
||||
function exports(val)
|
||||
{
|
||||
var type = typeof val;
|
||||
var regSpace = /^\s+/;
|
||||
|
||||
return !!val && (type === 'function' || type === 'object');
|
||||
function exports(str, chars)
|
||||
{
|
||||
if (chars == null) return str.replace(regSpace, '');
|
||||
|
||||
var start = 0,
|
||||
len = str.length,
|
||||
charLen = chars.length,
|
||||
found = true,
|
||||
i, c;
|
||||
|
||||
while (found && start < len)
|
||||
{
|
||||
found = false;
|
||||
i = -1;
|
||||
c = str.charAt(start);
|
||||
|
||||
while (++i < charLen)
|
||||
{
|
||||
if (c === chars[i])
|
||||
{
|
||||
found = true;
|
||||
start++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return start >= len ? '' : str.substr(start, len);
|
||||
}
|
||||
|
||||
return exports;
|
||||
@@ -752,5 +1165,418 @@ window._ = (function()
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ map ------------------------------ */
|
||||
|
||||
var map = _.map = (function ()
|
||||
{
|
||||
/* Create an array of values by running each element in collection through iteratee.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |--------|------------|------------------------------|
|
||||
* |obj |array object|Collection to iterate over |
|
||||
* |iteratee|function |Function invoked per iteration|
|
||||
* |[ctx] |* |Function context |
|
||||
* |return |array |New mapped array |
|
||||
*
|
||||
* ```javascript
|
||||
* map([4, 8], function (n) { return n * n; }); // -> [16, 64]
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* safeCb keys isArrLike
|
||||
*/
|
||||
|
||||
function exports(obj, iteratee, ctx)
|
||||
{
|
||||
iteratee = safeCb(iteratee, ctx);
|
||||
|
||||
var _keys = !isArrLike(obj) && keys(obj),
|
||||
len = (_keys || obj).length,
|
||||
results = Array(len);
|
||||
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
var curKey = _keys ? _keys[i] : i;
|
||||
results[i] = iteratee(obj[curKey], curKey, obj);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ rtrim ------------------------------ */
|
||||
|
||||
var rtrim = _.rtrim = (function ()
|
||||
{
|
||||
/* Remove chars or white-spaces from end of string.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|------------|------------------|
|
||||
* |str |string |String to trim |
|
||||
* |chars |string array|Characters to trim|
|
||||
* |return|string |Trimmed string |
|
||||
*
|
||||
* ```javascript
|
||||
* rtrim(' abc '); // -> ' abc'
|
||||
* rtrim('_abc_', '_'); // -> '_abc'
|
||||
* rtrim('_abc_', ['c', '_']); // -> '_ab'
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
var regSpace = /\s+$/;
|
||||
|
||||
function exports(str, chars)
|
||||
{
|
||||
if (chars == null) return str.replace(regSpace, '');
|
||||
|
||||
var end = str.length - 1,
|
||||
charLen = chars.length,
|
||||
found = true,
|
||||
i, c;
|
||||
|
||||
while (found && end >= 0)
|
||||
{
|
||||
found = false;
|
||||
i = -1;
|
||||
c = str.charAt(end);
|
||||
|
||||
while (++i < charLen)
|
||||
{
|
||||
if (c === chars[i])
|
||||
{
|
||||
found = true;
|
||||
end--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (end >= 0) ? str.substring(0, end + 1) : '';
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ trim ------------------------------ */
|
||||
|
||||
var trim = _.trim = (function ()
|
||||
{
|
||||
/* Remove chars or white-spaces from beginning end of string.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|------------|------------------|
|
||||
* |str |string |String to trim |
|
||||
* |chars |string array|Characters to trim|
|
||||
* |return|string |Trimmed string |
|
||||
*
|
||||
* ```javascript
|
||||
* trim(' abc '); // -> 'abc'
|
||||
* trim('_abc_', '_'); // -> 'abc'
|
||||
* trim('_abc_', ['a', 'c', '_']); // -> 'b'
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* ltrim rtrim
|
||||
*/
|
||||
|
||||
var regSpace = /^\s+|\s+$/g;
|
||||
|
||||
function exports(str, chars)
|
||||
{
|
||||
if (chars == null) return str.replace(regSpace, '');
|
||||
|
||||
return ltrim(rtrim(str, chars), chars);
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
/* ------------------------------ query ------------------------------ */
|
||||
|
||||
var query = _.query = (function (exports)
|
||||
{
|
||||
/* Parse and stringify url query strings.
|
||||
*
|
||||
* ### parse
|
||||
*
|
||||
* Parse a query string into an object.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|------|------------|
|
||||
* |str |string|Query string|
|
||||
* |return|object|Query object|
|
||||
*
|
||||
* ### stringify
|
||||
*
|
||||
* Stringify an object into a query string.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |------|------|------------|
|
||||
* |obj |object|Query object|
|
||||
* |return|string|Query string|
|
||||
*
|
||||
* ```javascript
|
||||
* query.parse('foo=bar&eruda=true'); // -> {foo: 'bar', eruda: 'true'}
|
||||
* query.stringify({foo: 'bar', eruda: 'true'}); // -> 'foo=bar&eruda=true'
|
||||
* query.parse('name=eruda&name=eustia'); // -> {name: ['eruda', 'eustia']}
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: all
|
||||
* test: all
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* trim each isUndef isArr map isEmpty filter isObj
|
||||
*/
|
||||
|
||||
exports = {
|
||||
parse: function (str)
|
||||
{
|
||||
var ret = {};
|
||||
|
||||
str = trim(str).replace(regIllegalChars, '');
|
||||
|
||||
each(str.split('&'), function (param)
|
||||
{
|
||||
var parts = param.split('=');
|
||||
|
||||
var key = parts.shift(),
|
||||
val = parts.length > 0 ? parts.join('=') : null;
|
||||
|
||||
key = decodeURIComponent(key);
|
||||
val = decodeURIComponent(val);
|
||||
|
||||
if (isUndef(ret[key]))
|
||||
{
|
||||
ret[key] = val;
|
||||
} else if (isArr(ret[key]))
|
||||
{
|
||||
ret[key].push(val);
|
||||
} else
|
||||
{
|
||||
ret[key] = [ret[key], val];
|
||||
}
|
||||
});
|
||||
|
||||
return ret;
|
||||
},
|
||||
stringify: function (obj, arrKey)
|
||||
{
|
||||
return filter(map(obj, function (val, key)
|
||||
{
|
||||
if (isObj(val) && isEmpty(val)) return '';
|
||||
if (isArr(val)) return exports.stringify(val, key);
|
||||
|
||||
return (arrKey ? encodeURIComponent(arrKey) : encodeURIComponent(key)) + '=' + encodeURIComponent(val);
|
||||
}), function (str)
|
||||
{
|
||||
return str.length > 0;
|
||||
}).join('&');
|
||||
}
|
||||
};
|
||||
|
||||
var regIllegalChars = /^(\?|#|&)/g;
|
||||
|
||||
return exports;
|
||||
})({});
|
||||
|
||||
/* ------------------------------ ajax ------------------------------ */
|
||||
|
||||
_.ajax = (function ()
|
||||
{
|
||||
/* Perform an asynchronous HTTP request.
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |-------|------|------------|
|
||||
* |options|object|Ajax options|
|
||||
*
|
||||
* Available options:
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |-------------|-------------|------------------------|
|
||||
* |url |string |Request url |
|
||||
* |data |string object|Request data |
|
||||
* |dataType=json|string |Response type(json, xml)|
|
||||
* |success |function |Success callback |
|
||||
* |error |function |Error callback |
|
||||
* |complete |function |Callback after request |
|
||||
* |timeout |number |Request timeout |
|
||||
*
|
||||
* ### get
|
||||
*
|
||||
* Shortcut for type = GET;
|
||||
*
|
||||
* ### post
|
||||
*
|
||||
* Shortcut for type = POST;
|
||||
*
|
||||
* |Name |Type |Desc |
|
||||
* |--------|-------------|----------------|
|
||||
* |url |string |Request url |
|
||||
* |[data] |string object|Request data |
|
||||
* |success |function |Success callback|
|
||||
* |dataType|function |Response type |
|
||||
*
|
||||
* ```javascript
|
||||
* ajax({
|
||||
* url: 'http://example.com',
|
||||
* data: {test: 'true'},
|
||||
* error: function () {},
|
||||
* success: function (data)
|
||||
* {
|
||||
* // ...
|
||||
* },
|
||||
* dataType: 'json'
|
||||
* });
|
||||
*
|
||||
* ajax.get('http://example.com', {}, function (data)
|
||||
* {
|
||||
* // ...
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
|
||||
/* module
|
||||
* env: browser
|
||||
* test: manual
|
||||
*/
|
||||
|
||||
/* dependencies
|
||||
* isFn noop defaults isObj query
|
||||
*/
|
||||
|
||||
function exports(options)
|
||||
{
|
||||
defaults(options, exports.setting);
|
||||
|
||||
var type = options.type,
|
||||
url = options.url,
|
||||
data = options.data,
|
||||
dataType = options.dataType,
|
||||
success = options.success,
|
||||
error = options.error,
|
||||
timeout = options.timeout,
|
||||
complete = options.complete,
|
||||
xhr = options.xhr(),
|
||||
abortTimeout;
|
||||
|
||||
xhr.onreadystatechange = function ()
|
||||
{
|
||||
if (xhr.readyState !== 4) return;
|
||||
|
||||
clearTimeout(abortTimeout);
|
||||
|
||||
var result;
|
||||
|
||||
var status = xhr.status;
|
||||
if ((status >= 200 && status < 300) || status === 304)
|
||||
{
|
||||
result = xhr.responseText;
|
||||
if (dataType === 'xml') result = xhr.responseXML;
|
||||
try {
|
||||
if (dataType === 'json') result = JSON.parse(result);
|
||||
/* eslint-disable no-empty */
|
||||
} catch (e) {}
|
||||
success(result, xhr);
|
||||
} else
|
||||
{
|
||||
error(xhr);
|
||||
}
|
||||
|
||||
complete(xhr);
|
||||
};
|
||||
|
||||
if (type === 'GET')
|
||||
{
|
||||
data = query.stringify(data);
|
||||
url += url.indexOf('?') > -1 ? '&' + data : '?' + data;
|
||||
} else
|
||||
{
|
||||
if(isObj(data)) data = query.stringify(data);
|
||||
}
|
||||
|
||||
xhr.open(type, url, true);
|
||||
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||||
|
||||
if (timeout > 0)
|
||||
{
|
||||
abortTimeout = setTimeout(function ()
|
||||
{
|
||||
xhr.onreadystatechange = noop;
|
||||
xhr.abort();
|
||||
error(xhr, 'timeout');
|
||||
complete(xhr);
|
||||
}, timeout);
|
||||
}
|
||||
xhr.send(type === 'GET' ? null : data);
|
||||
|
||||
return xhr;
|
||||
}
|
||||
|
||||
exports.setting = {
|
||||
type: 'GET',
|
||||
success: noop,
|
||||
error: noop,
|
||||
complete: noop,
|
||||
dataType: 'json',
|
||||
data: {},
|
||||
xhr: function () { return new XMLHttpRequest() },
|
||||
timeout: 0
|
||||
};
|
||||
|
||||
exports.get = function ()
|
||||
{
|
||||
return exports(parseArgs.apply(null, arguments));
|
||||
};
|
||||
|
||||
exports.post = function ()
|
||||
{
|
||||
var options = parseArgs.apply(null, arguments);
|
||||
options.type = 'POST';
|
||||
|
||||
return exports(options);
|
||||
};
|
||||
|
||||
function parseArgs(url, data, success, dataType)
|
||||
{
|
||||
if (isFn(data))
|
||||
{
|
||||
dataType = success;
|
||||
success = data;
|
||||
data = {};
|
||||
}
|
||||
|
||||
return {
|
||||
url: url,
|
||||
data: data,
|
||||
success: success,
|
||||
dataType: dataType
|
||||
};
|
||||
}
|
||||
|
||||
return exports;
|
||||
})();
|
||||
|
||||
return _;
|
||||
})();
|
||||
Reference in New Issue
Block a user