Skip to content

Commit a5908c0

Browse files
Fix bug where numbers and strings were not differentiated (Fixes issue qiao#10).
Update build scripts and npm dependencies. Add unit test for issue qiao#10.
1 parent e11553b commit a5908c0

File tree

8 files changed

+1040
-895
lines changed

8 files changed

+1040
-895
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ test:
1212
node_modules/.bin/mocha \
1313
--ui qunit \
1414
--require should \
15+
--require coffeescript/register \
1516
--timeout $(TEST_TIMEOUT) \
1617
--reporter $(TEST_REPORTER) \
17-
--compilers coffee:coffee-script \
1818
test/*.coffee
1919

2020
.PHONY: test

dist/difflib-browser.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/difflib.js

Lines changed: 973 additions & 848 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
{
2-
"name" : "difflib"
3-
, "version" : "0.2.4"
4-
, "description" : "text diff library ported from Python's difflib module"
5-
, "homepage" : "https://github.com/qiao/difflib.js"
6-
, "keywords" : ["diff"]
7-
, "author" : "Xueqiao Xu <[email protected]>"
8-
, "main" : "./index.js"
9-
, "dependencies" : {
10-
"heap" : ">= 0.2.0"
11-
}
12-
, "devDependencies" : {
13-
"coffee-script" : "1.3.x"
14-
, "mocha" : "1.0.x"
15-
, "should" : "0.6.x"
16-
, "browserify" : "1.10.x"
17-
, "uglify-js" : ">= 1.2.5"
18-
}
19-
, "repository" : {
20-
"type" : "git"
21-
, "url" : "git://github.com/qiao/difflib.js.git"
22-
}
23-
, "licenses" : [{
24-
"type" : "PSF"
25-
, "url" : "http://docs.python.org/license.html"
26-
}]
2+
"name": "difflib",
3+
"version": "0.2.4",
4+
"description": "text diff library ported from Python's difflib module",
5+
"homepage": "https://github.com/qiao/difflib.js",
6+
"keywords": [
7+
"diff"
8+
],
9+
"author": "Xueqiao Xu <[email protected]>",
10+
"main": "./index.js",
11+
"dependencies": {
12+
"browserify-fs": "^1.0.0",
13+
"heap": ">= 0.2.0"
14+
},
15+
"devDependencies": {
16+
"browserify": "17.0.x",
17+
"coffeescript": "2.6.x",
18+
"mocha": "9.1.x",
19+
"should": "13.2.x",
20+
"uglify-js": ">= 3.14.x"
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "git://github.com/qiao/difflib.js.git"
25+
},
26+
"licenses": [
27+
{
28+
"type": "PSF",
29+
"url": "http://docs.python.org/license.html"
30+
}
31+
]
2732
}

src/difflib.coffee

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,37 +310,38 @@ class SequenceMatcher
310310
# out the junk later is much cheaper than building b2j "right"
311311
# from the start.
312312
b = @b
313-
@b2j = b2j = {}
313+
@b2j = b2j = new Map()
314314

315315
for elt, i in b
316-
indices = if _has(b2j, elt) then b2j[elt] else b2j[elt] = []
316+
if !b2j.has(elt) then b2j.set(elt, [])
317+
indices = b2j.get(elt)
317318
indices.push(i)
318319

319320
# Purge junk elements
320-
junk = {}
321+
junk = new Map()
321322
isjunk = @isjunk
322323
if isjunk
323-
for elt in Object.keys(b2j)
324+
b2j.forEach (idxs, elt) ->
324325
if isjunk(elt)
325-
junk[elt] = true
326-
delete b2j[elt]
326+
junk.set(elt, true)
327+
b2j.delete(elt)
327328

328329
# Purge popular elements that are not junk
329-
popular = {}
330+
popular = new Map()
330331
n = b.length
331332
if @autojunk and n >= 200
332333
ntest = floor(n / 100) + 1
333-
for elt, idxs of b2j
334+
b2j.forEach (idxs, elt) ->
334335
if idxs.length > ntest
335-
popular[elt] = true
336-
delete b2j[elt]
336+
popular.set(elt, true)
337+
b2j.delete(elt)
337338

338339
# Now for x in b, isjunk(x) == x in junk, but the latter is much faster.
339340
# Sicne the number of *unique* junk elements is probably small, the
340341
# memory burden of keeping this set alive is likely trivial compared to
341342
# the size of b2j.
342-
@isbjunk = (b) -> _has(junk, b)
343-
@isbpopular = (b) -> _has(popular, b)
343+
@isbjunk = (b) -> junk.has(b)
344+
@isbpopular = (b) -> popular.has(b)
344345

345346
findLongestMatch: (alo, ahi, blo, bhi) ->
346347
###
@@ -393,7 +394,10 @@ class SequenceMatcher
393394
# look at all instances of a[i] in b; note that because
394395
# b2j has no junk keys, the loop is skipped if a[i] is junk
395396
newj2len = {}
396-
for j in (if _has(b2j, a[i]) then b2j[a[i]] else [])
397+
jlist = []
398+
if b2j.has(a[i])
399+
jlist = b2j.get(a[i])
400+
for j in jlist
397401
# a[i] matches b[j]
398402
continue if j < blo
399403
break if j >= bhi

test/SequenceMatcher.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ test '#setSeq2', ->
1919
s.setSeq2('abcd')
2020
s.ratio().should.eql 1.0
2121

22+
test '#stringsAndIntsAreDifferent', ->
23+
opcodes = new SequenceMatcher(null, ['1','b','c','d'], [1, 'b','c','d']).getOpcodes()
24+
opcodes.should.eql [ [ 'replace', 0, 1, 0, 1 ], [ 'equal', 1, 4, 1, 4 ] ]
25+
2226
test '#findLongestMatch', ->
2327
isjunk = (x) -> x is ' '
2428
s = new SequenceMatcher(isjunk, ' abcd', 'abcd abcd')

test/global.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ test '.getCloseMatches', ->
2828
getCloseMatches('appel', ['ape', 'apple', 'peach', 'puppy'])
2929
.should.eql ['apple', 'ape']
3030

31-
KEYWORDS = require('coffee-script').RESERVED
31+
# KEYWORDS = require('coffeescript').RESERVED # -- no longer available
32+
KEYWORDS = ['case', 'default', 'function', 'var', 'void', 'with', 'const', 'let', 'enum', 'export', 'import', 'native', '__hasProp', '__extends', '__slice', '__bind', '__indexOf', 'implements', 'else', 'interface', 'package', 'private', 'protected', 'public', 'static', 'yield', 'true', 'false', 'null', 'this', 'new', 'delete', 'typeof', 'in', 'arguments', 'eval', 'instanceof', 'return', 'throw', 'break', 'continue', 'debugger', 'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally', 'class', 'extends', 'super', 'undefined', 'then', 'unless', 'until', 'loop', 'of', 'by', 'when', 'and', 'or', 'is', 'isnt', 'not', 'yes', 'no', 'on', 'off']
3233
getCloseMatches('wheel', KEYWORDS).should.eql ['when', 'while']
3334
getCloseMatches('accost', KEYWORDS).should.eql ['const']
3435

util/build.coffee

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ BANNER = '''
1414
'''
1515

1616
build = (dest) ->
17-
browserified = browserify.bundle(__dirname + '/../lib/difflib.js')
18-
namespaced = 'var difflib = (function() {' + browserified + 'return require("/difflib");})();'
19-
uglified = uglify(namespaced)
20-
bannered = BANNER + uglified
21-
fs.writeFileSync(dest, bannered)
22-
17+
bify = browserify()
18+
bify.add(__dirname + '/../lib/difflib.js')
19+
bify.bundle((err, payload) ->
20+
browserified = payload.toString('utf8')
21+
namespaced = 'var difflib = (function() {' + browserified + 'return require("/difflib");})();'
22+
uglified = uglify.minify(namespaced).code
23+
bannered = BANNER + uglified
24+
fs.writeFileSync(dest, bannered)
25+
)
26+
2327
build(__dirname + '/../dist/difflib-browser.js')

0 commit comments

Comments
 (0)