Skip to content

Commit f574655

Browse files
committed
add license.
1 parent a34273c commit f574655

File tree

5 files changed

+51
-48
lines changed

5 files changed

+51
-48
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT LICENSE
2+
3+
Copyright (c) 2018-present Alibaba Group Holding Limited. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
# The diff algorithm of Patch.js
22

3+
## Installation
4+
5+
```bash
6+
npm install patchjs-diff --save-dev
7+
```
8+
9+
## API
10+
11+
calcDiffData (oldString, newString)
12+
13+
## Algorithm
14+
15+
16+
17+
318

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "patch-diff",
2+
"name": "patchjs-diff",
33
"description": "The diff algorithm of Patch.js",
44
"version": "0.1.0",
55
"author": {
@@ -10,6 +10,11 @@
1010
"patch",
1111
"patchjs"
1212
],
13+
"repository" : {
14+
"type" : "git",
15+
"url" : "https://github.com/patchjs/patchjs-diff.git"
16+
},
17+
"license": "MIT",
1318
"dependencies": {
1419
"babel-cli": "^6.24.0",
1520
"babel-core": "^6.24.0",
@@ -29,9 +34,6 @@
2934
"prepublish": "npm run build",
3035
"precommit": "npm run lint & npm run test"
3136
},
32-
"publishConfig": {
33-
"registry": "http://registry.npm.alibaba-inc.com"
34-
},
3537
"devDependencies": {
3638
"co": "^4.6.0",
3739
"eslint": "^3.19.0",

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default function calcDiffData (localFileContent, fileContent) {
1414
return result;
1515
}
1616

17-
const oldMd5Map = checksum(localFileContent, CHUNK_SIZE);
18-
const diffArray = roll(newFileContent, oldMd5Map, CHUNK_SIZE);
17+
const localMd5Map = checksum(localFileContent, CHUNK_SIZE);
18+
const diffArray = roll(fileContent, localMd5Map, CHUNK_SIZE);
1919
let arrayData = '';
2020
let lastItem = null;
2121
let matchedCount = 0;
@@ -56,7 +56,7 @@ export default function calcDiffData (localFileContent, fileContent) {
5656
return result;
5757
}
5858

59-
function roll (content, oldMd5Map, chunkSize) {
59+
function roll (content, localMd5Map, chunkSize) {
6060
let diffDataArray = [];
6161
let buffer = '';
6262
let outBuffer = '';
@@ -71,7 +71,7 @@ function roll (content, oldMd5Map, chunkSize) {
7171
}
7272
buffer = content.substring(currentIndex, endIndex);
7373
const chunkMd5 = md5(buffer);
74-
const matchedNo = findMatchedNo(chunkMd5, oldMd5Map, lastMatchNo);
74+
const matchedNo = findMatchedNo(chunkMd5, localMd5Map, lastMatchNo);
7575

7676
if (endIndex > len - 1) {
7777
if (outBuffer.length > 0 && outBuffer !== '') {
@@ -134,8 +134,8 @@ function checksum (content, chunkSize) {
134134
return md5Map;
135135
}
136136

137-
function findMatchedNo (chunkMd5, oldMd5Map, lastMatchNo) {
138-
let numArray = oldMd5Map[chunkMd5];
137+
function findMatchedNo (chunkMd5, localMd5Map, lastMatchNo) {
138+
let numArray = localMd5Map[chunkMd5];
139139
if (!numArray) {
140140
return -1;
141141
} else {

test/index.js

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import expect from 'expect.js';
33
import calcDiffData from '../src/index.js';
44
import fs from 'fs';
5-
import urllib from 'urllib';
6-
import co from 'co';
75

86
const testPath = __dirname;
97

@@ -22,29 +20,15 @@ function mergeCode (source, chunkSize, diffCodeArray) {
2220
return jsCode;
2321
}
2422

25-
function request (url, callback) {
26-
co(function * () {
27-
const result = yield urllib.requestThunk(url);
28-
let content = result.data.toString();
29-
if (result.status === 200) {
30-
callback(content);
31-
} else {
32-
callback(`Not Found: ${url}`);
33-
}
34-
}).catch((e) => {
35-
callback(e);
36-
console.error(e);
37-
});
38-
}
39-
4023
describe('index.js', () => {
41-
it('calcDiffData (oldFileContent, newFileContent)', () => {
24+
it('calcDiffData (localFileContent, fileContent)', () => {
4225
// same content
4326
expect(calcDiffData('var num = 0;', 'var num = 0;')).to.eql({
4427
l: 20,
4528
m: false,
4629
c: []
4730
});
31+
4832
// replace content
4933
expect(calcDiffData('var num = 0;var str = "string1";', 'var num = 1;var str = "string2";')).to.eql({
5034
l: 20,
@@ -67,26 +51,6 @@ describe('index.js', () => {
6751
const source = fs.readFileSync(`${testPath}/data/antd-mobile.min.js`, {encoding: 'utf8'});
6852
const target = fs.readFileSync(`${testPath}/data/antd-mobile.min.change.js`, {encoding: 'utf8'});
6953
const diffResult = calcDiffData(source, target);
70-
console.log(diffResult);
7154
expect(mergeCode(source, diffResult.l, diffResult.c)).to.eql(target);
72-
// console.log(mergeCode(source, diffResult.l, diffResult.c));
73-
});
74-
75-
it('mergeCDNCode ()', (done) => {
76-
const urls = [
77-
'http://alipay-rmsdeploy-assets-pre.cn-hangzhou.alipay.aliyun-inc.com/g/kbservcenter/kb-m-data/1.3.0/index.js',
78-
'http://alipay-rmsdeploy-assets-pre.cn-hangzhou.alipay.aliyun-inc.com/g/kbservcenter/kb-m-data/1.3.1/index.js',
79-
'http://alipay-rmsdeploy-assets-pre.cn-hangzhou.alipay.aliyun-inc.com/g/kbservcenter/kb-m-data/1.3.1/index-1.3.0.js'
80-
];
81-
82-
request(urls[0], (source) => {
83-
request(urls[1], (target) => {
84-
request(urls[2], (diffResult) => {
85-
diffResult = JSON.parse(diffResult);
86-
expect(mergeCode(source, diffResult.l, diffResult.c)).to.eql(target);
87-
done();
88-
});
89-
});
90-
});
9155
});
9256
});

0 commit comments

Comments
 (0)