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

Commit f3c9ae2

Browse files
committed
使用 strandjs 风格并添加 eslint
1 parent 11f3f1d commit f3c9ae2

File tree

7 files changed

+248
-231
lines changed

7 files changed

+248
-231
lines changed

.eslintrc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"globals": {
3+
"chrome": false,
4+
"ES6Promise": false,
5+
"chromeCall": false
6+
},
7+
"env": {
8+
"browser": true,
9+
"amd": true,
10+
"jasmine": true
11+
},
12+
"plugins": [
13+
"html"
14+
],
15+
"extends": [
16+
"standard"
17+
]
18+
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22

33
node_js:
4-
- "4"
4+
- "node"
55

66
before_install:
77
- npm install -g npm

chrome-call.js

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
(function ( root , factory ) {
1+
(function (root, factory) {
22
/* istanbul ignore next */
3-
if ( typeof module === 'object' && typeof module.exports === 'object' ) {
4-
module.exports = factory();
5-
} else if ( typeof define === 'function' && define.amd ) {
6-
define( [] , factory );
3+
if (typeof module === 'object' && typeof module.exports === 'object') {
4+
module.exports = factory()
5+
} else if (typeof define === 'function' && define.amd) {
6+
define([], factory)
77
} else {
8-
root[ 'chromeCall' ] = factory();
8+
root['chromeCall'] = factory()
99
}
10-
})( this , function () {
11-
'use strict';
12-
var runtime = chrome.runtime;
10+
})(this, function () {
11+
'use strict'
12+
var runtime = chrome.runtime
1313

1414
/**
1515
* 根据 base 对象返回指定路径的栈
@@ -20,88 +20,89 @@
2020
* @example
2121
* pathStack('document.body',window) 应该返回 [window,window.document,window.document.body]
2222
*/
23-
function pathStack( path , base ) {
24-
var keys = path.split( '.' ) ,
25-
stack = [ base ];
23+
function pathStack (path, base) {
24+
var keys = path.split('.')
25+
var stack = [base]
2626

27-
keys.forEach( function ( key , i ) {
28-
var val = stack[ i ][ key ];
29-
if ( !val ) {
30-
throw new Error( 'Cannot find "' + path + '".' );
27+
keys.forEach(function (key, i) {
28+
var val = stack[i][key]
29+
if (!val) {
30+
throw new Error('Cannot find "' + path + '".')
3131
}
32-
stack.push( val );
33-
} );
32+
stack.push(val)
33+
})
3434

35-
return stack;
35+
return stack
3636
}
3737

3838
/**
3939
* 根据基础对象返回一个函数,此函数会以基础对象为起点寻找指定属性并调用
4040
* @param {String|Object} base
4141
* @returns {Function}
4242
*/
43-
function scope( base ) {
44-
var baseObj = typeof base === 'string' ? pathStack( base , chrome ).pop() : base;
43+
function scope (base) {
44+
var baseObj = typeof base === 'string' ? pathStack(base, chrome).pop() : base
4545

4646
/**
4747
* 调用原本的 chrome api 并返回一个 Promise
4848
* @param {Boolean} [returnArray] - 当函数的第一个值是 true 时,则 Promise 会返回一个数组,包含 callback 的所有参数
4949
* @param {String} fnPath - 原本的 chrome api 的路径,相对于 chrome,如 storage.local.get
5050
* @returns {Promise}
5151
*/
52-
return function ( /*returnArray , fnPath , ...args*/ ) {
53-
// inline copy arguments, see https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments
54-
var length = arguments.length ,
55-
argumentsArray = [];
52+
return function (/* returnArray , fnPath , ...args */) {
53+
// inline copy arguments,
54+
// see https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments
55+
var length = arguments.length
56+
var argumentsArray = []
5657

57-
for ( var i = 0 ; i < length ; i += 1 ) {
58-
argumentsArray[ i ] = arguments[ i ];
58+
for (var i = 0; i < length; i += 1) {
59+
argumentsArray[i] = arguments[i]
5960
}
6061

61-
var returnArray = argumentsArray.shift() ,
62-
fnPath;
62+
var returnArray = argumentsArray.shift()
63+
var fnPath
6364

64-
if ( typeof returnArray === 'boolean' ) {
65-
fnPath = argumentsArray.shift();
65+
if (typeof returnArray === 'boolean') {
66+
fnPath = argumentsArray.shift()
6667
} else {
67-
fnPath = returnArray;
68-
returnArray = false;
68+
fnPath = returnArray
69+
returnArray = false
6970
}
7071

7172
// Step 1: find the function which need to be call
72-
var stack = pathStack( fnPath , baseObj );
73+
var stack = pathStack(fnPath, baseObj)
7374

74-
var args = argumentsArray;
75-
return new Promise( function ( resolve , reject ) {
75+
var args = argumentsArray
76+
return new Promise(function (resolve, reject) {
7677
// Step 2: inject callback
77-
args.push( function () {
78-
var lastError = runtime.lastError;
79-
if ( lastError ) {
80-
reject( lastError );
81-
return;
78+
args.push(function () {
79+
var lastError = runtime.lastError
80+
if (lastError) {
81+
reject(lastError)
82+
return
8283
}
8384

84-
if ( returnArray ) {
85-
var length = arguments.length ,
86-
argumentsArray = [];
85+
if (returnArray) {
86+
var length = arguments.length
87+
var argumentsArray = []
8788

88-
for ( var i = 0 ; i < length ; i += 1 ) {
89-
argumentsArray[ i ] = arguments[ i ];
89+
for (var i = 0; i < length; i += 1) {
90+
argumentsArray[i] = arguments[i]
9091
}
9192

92-
resolve( argumentsArray );
93+
resolve(argumentsArray)
9394
} else {
94-
resolve( arguments[ 0 ] );
95+
resolve(arguments[0])
9596
}
96-
} );
97+
})
9798

9899
// Step 3: call function with it's original "this"
99-
stack.pop().apply( stack.pop() , args );
100-
} );
101-
};
100+
stack.pop().apply(stack.pop(), args)
101+
})
102+
}
102103
}
103104

104-
var defaultCp = scope( chrome );
105-
defaultCp.scope = scope;
106-
return defaultCp;
107-
} );
105+
var defaultCp = scope(chrome)
106+
defaultCp.scope = scope
107+
return defaultCp
108+
})

karma.conf.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
module.exports = function ( config ) {
2-
config.set( {
3-
basePath : '' ,
4-
frameworks : [ 'jasmine' ] ,
5-
files : [
6-
'node_modules/es6-promise/dist/es6-promise.js' ,
7-
'test/helper.js' ,
8-
'chrome-call.js' ,
1+
module.exports = function (config) {
2+
config.set({
3+
basePath: '',
4+
frameworks: ['jasmine'],
5+
files: [
6+
'node_modules/es6-promise/dist/es6-promise.js',
7+
'test/helper.js',
8+
'chrome-call.js',
99
'test/test.js'
10-
] ,
11-
preprocessors : {
12-
'chrome-call.js' : [ 'coverage' ]
13-
} ,
14-
reporters : [ 'progress' , 'coverage' ] ,
15-
coverageReporter : {
16-
dir : 'coverage' ,
17-
reporters : [
10+
],
11+
preprocessors: {
12+
'chrome-call.js': ['coverage']
13+
},
14+
reporters: ['progress', 'coverage'],
15+
coverageReporter: {
16+
dir: 'coverage',
17+
reporters: [
1818
{
19-
type : 'html' ,
20-
subdir : function ( browser ) {
21-
return 'html/' + browser.toLowerCase().split( /[ /-]/ )[ 0 ];
19+
type: 'html',
20+
subdir: function (browser) {
21+
return 'html/' + browser.toLowerCase().split(/[ /-]/)[0]
2222
}
23-
} ,
23+
},
2424
{
25-
type : 'lcov' ,
26-
subdir : 'lcov'
25+
type: 'lcov',
26+
subdir: 'lcov'
2727
}
2828
]
29-
} ,
30-
port : 9876 ,
31-
colors : true ,
32-
logLevel : config.LOG_INFO ,
33-
autoWatch : false ,
34-
browsers : [ 'Firefox' , 'Chrome' , 'IE' , 'PhantomJS' ] ,
35-
singleRun : true
36-
} )
37-
};
29+
},
30+
port: 9876,
31+
colors: true,
32+
logLevel: config.LOG_INFO,
33+
autoWatch: false,
34+
browsers: ['Chrome', 'PhantomJS'],
35+
singleRun: true
36+
})
37+
}

package.json

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@
1111
"url": "https://github.com/lmk123/chrome-call/issues"
1212
},
1313
"scripts": {
14-
"test": "karma start"
14+
"test": "karma start",
15+
"lint": "eslint --ignore-pattern '/dist/' \"**/*.{js,vue}\""
1516
},
1617
"devDependencies": {
17-
"es6-promise": "^3.0.2",
18-
"phantomjs": "^1.9.18",
19-
"jasmine-core": "^2.4.1",
20-
"karma": "^0.13.14",
21-
"karma-chrome-launcher": "^0.2.1",
22-
"karma-firefox-launcher": "^0.1.6",
23-
"karma-ie-launcher": "^0.2.0",
24-
"karma-jasmine": "^0.3.6",
25-
"karma-phantomjs-launcher": "^0.2.1",
26-
"karma-safari-launcher": "^0.1.1",
27-
"karma-coverage": "^0.5.3",
28-
"karma-coveralls": "^1.1.2"
18+
"es6-promise": "4.0.5",
19+
"phantomjs-prebuilt": "2.1.13",
20+
"jasmine-core": "2.5.2",
21+
"karma": "1.3.0",
22+
"karma-chrome-launcher": "2.0.0",
23+
"karma-jasmine": "1.0.2",
24+
"karma-phantomjs-launcher": "1.0.2",
25+
"karma-safari-launcher": "1.0.0",
26+
"karma-coverage": "1.1.1",
27+
"karma-coveralls": "1.1.2",
28+
"eslint": "3.8.1",
29+
"eslint-config-standard": "6.2.0",
30+
"eslint-plugin-promise": "3.3.0",
31+
"eslint-plugin-standard": "2.0.1"
2932
},
3033
"keywords": [
3134
"chrome",

test/helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ES6Promise.polyfill();
1+
ES6Promise.polyfill()
22
window.chrome = {
3-
runtime : {}
4-
};
3+
runtime: {}
4+
}

0 commit comments

Comments
 (0)