Skip to content

Commit dbc1d9d

Browse files
committed
replace constant generator algoritm
add unit test add phantom and node environment test
1 parent 77870a4 commit dbc1d9d

File tree

9 files changed

+325
-9
lines changed

9 files changed

+325
-9
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_js:
33
- "5"
44
script:
55
- npm run lint
6-
- npm test
6+
- npm test
7+
- npm run test-node

karma.conf.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var webpack = require('webpack');
2+
var devConfig = require('./webpack.config.base');
3+
4+
module.exports = function (config) {
5+
config.set({
6+
7+
// browsers: [ process.env.CONTINUOUS_INTEGRATION ? 'Firefox' : 'Chrome' ],
8+
browsers: ['PhantomJS'],
9+
10+
frameworks: [ 'mocha', 'phantomjs-shim' ],
11+
12+
reporters: [ 'dots' ],
13+
14+
files: [
15+
"test/**/*.js"
16+
],
17+
18+
preprocessors: {
19+
'test/**/*.js': [ 'webpack', 'sourcemap' ]
20+
},
21+
22+
webpack: devConfig,
23+
});
24+
};

log.txt

Lines changed: 224 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-constant",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "Fuck off constants.js and golobal constants in react/flux/redux/wateva.",
55
"main": "lib/index.js",
66
"files": [
@@ -11,8 +11,10 @@
1111
"scripts": {
1212
"clean": "rimraf lib dist coverage",
1313
"lint": "eslint src test examples",
14-
"test": "mocha --compilers js:babel-core/register --recursive",
15-
"test:watch": "npm test -- --watch",
14+
"test":"karma start --single-run",
15+
"test-node": "mocha --compilers js:babel-core/register --recursive",
16+
"test:watch": "karma start karma.conf.js --auto-watch",
17+
"test-node:watch": "mocha --compilers js:babel-core/register --recursive --watch",
1618
"check": "npm run lint && npm run test",
1719
"build:lib": "babel src --out-dir lib",
1820
"build:umd": "webpack src/index.js dist/constant.js --config webpack.config.development.js",
@@ -45,9 +47,15 @@
4547
"babel-preset-stage-0": "^6.1.18",
4648
"eslint": "^1.10.1",
4749
"eslint-config-rackt": "^1.1.1",
50+
"karma": "^0.13.15",
51+
"karma-mocha": "^0.2.1",
52+
"karma-phantomjs-launcher": "^0.2.1",
53+
"karma-phantomjs-shim": "^1.0.0",
54+
"karma-sourcemap-loader": "^0.3.5",
55+
"karma-webpack": "^1.7.0",
4856
"mocha": "^2.3.4",
57+
"phantomjs": "^1.9.19",
4958
"rimraf": "^2.4.4",
50-
"should": "^7.1.1",
51-
"webpack": "^1.12.8"
59+
"should": "^7.1.1"
5260
}
5361
}

src/utils/Constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import uuidGenerator from './uuidGenerator';
1+
import {randomBase64} from './fnUtils';
22

33
/**
44
* generate and return a constant
@@ -15,7 +15,7 @@ export default class Constants {
1515
if(typeof this[key] !== 'undefined') {
1616
return this[key];
1717
}
18-
this[key] = `${this.namespace}-${key}-${uuidGenerator()}`;
18+
this[key] = `${this.namespace}-${key}-${randomBase64()}`;
1919
return this[key];
2020
}
2121
}

src/utils/fnUtils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Generate random base 64 which length
3+
* determinate by param , default to 5;
4+
* @param {...[number, number]} arg slice start index
5+
* and end index
6+
* @return {String} encoded string
7+
*/
8+
export function randomBase64(...arg){
9+
let start = typeof arg[0] === 'number'?arg[0]:-5;
10+
let end = typeof arg[1] === 'number'?arg[1]:undefined;
11+
let codedString ;
12+
if('object' === typeof process && Object.prototype.toString.call(process) === '[object process]'){
13+
codedString = new Buffer(`${Math.random()}`.slice(2))
14+
.toString('base64')
15+
.replace(/\=/g,'')
16+
.slice(start, end);
17+
}else if('undefined' !==typeof btoa){
18+
codedString = btoa(Math.random())
19+
.replace('=')
20+
.slice(start, end);
21+
}else{
22+
codedString = '';
23+
}
24+
25+
if(codedString.length>20){
26+
codedString = codedString.slice(0,20)
27+
}
28+
return codedString;
29+
}

test/utils/fnUtils.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {randomBase64} from '../../src/utils/fnUtils';
2+
import should from 'should';
3+
4+
describe('fnUtils', () => {
5+
it('get different value in every invoke', () => {
6+
let tmp = [];
7+
const base64Encoded = randomBase64();
8+
for(let i=0; i<10;i++){
9+
tmp.push(randomBase64())
10+
}
11+
let index = tmp.indexOf(base64Encoded);
12+
should(index).lessThan(0);
13+
});
14+
15+
it('fnUtils should return 5 chars default', () => {
16+
const base64Encoded = randomBase64();
17+
should(base64Encoded.length).equal(5);
18+
});
19+
20+
it('should return spec number by spec param', () => {
21+
const base64Encoded = randomBase64(0, 6);
22+
should(base64Encoded.length).equal(6);
23+
})
24+
25+
it('should return no more than 20 chars', () => {
26+
const base64Encoded = randomBase64(0);
27+
should(base64Encoded.length).belowOrEqual(20);
28+
})
29+
})

webpack.config.base.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ module.exports = {
1212
},
1313
resolve: {
1414
extensions: ['', '.js']
15-
}
15+
},
16+
devtool: 'source-map',
1617
};

0 commit comments

Comments
 (0)