Skip to content

Commit 6437124

Browse files
committed
Use "older" standards; add tests
- Function parameter default value introduced in ECMA 2015 - `array.includes()` not used anywhere, use `array.indexOf()` instead
1 parent 3750994 commit 6437124

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

core/modules/filters/kin.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,55 @@ Finds out where a tiddler originates from and what other tiddlers originate from
1717
titlesPointingToBase = [];
1818

1919
function addToResultsIfNotFoundAlready(list,title) {
20-
if(list.includes(title)) {
20+
if(list.indexOf(title) !== -1) {
2121
return false;
2222
}
2323
list.push(title);
2424
return true
2525
}
2626

27-
function collectTitlesPointingFrom(tiddler,title,currentDepth = 0) {
27+
function collectTitlesPointingFrom(tiddler,title,currentDepth) {
2828
if((options.depth) && (currentDepth++ > options.depth)) {
2929
return;
3030
}
3131
if(addToResultsIfNotFoundAlready(titlesPointingFromBase,title)) {
3232
if(tiddler) {
3333
$tw.utils.each(tiddler.getFieldList(options.fieldName),function(targetTitle) {
34-
collectTitlesPointingFrom($tw.wiki.getTiddler(targetTitle),targetTitle,currentDepth);
34+
collectTitlesPointingFrom(options.wiki.getTiddler(targetTitle),targetTitle,currentDepth);
3535
});
3636
}
3737
}
3838
}
3939

40-
function collectTitlesPointingTo(title,currentDepth = 0) {
40+
function collectTitlesPointingTo(title,currentDepth) {
4141
if((options.depth) && (currentDepth++ > options.depth)) {
4242
return;
4343
}
4444
if(addToResultsIfNotFoundAlready(titlesPointingToBase,title)) {
45-
$tw.utils.each($tw.wiki.findListingsOfTiddler(title,options.fieldName),function(targetTitle) {
45+
$tw.utils.each(options.wiki.findListingsOfTiddler(title,options.fieldName),function(targetTitle) {
4646
collectTitlesPointingTo(targetTitle,currentDepth);
4747
});
4848
}
4949
}
5050

5151
if((options.direction === "from") || (options.direction === "with")) {
52-
collectTitlesPointingFrom(baseTiddler,baseTitle);
52+
collectTitlesPointingFrom(baseTiddler,baseTitle,0);
5353
}
5454
if((options.direction === "to") || (options.direction === "with")) {
55-
collectTitlesPointingTo(baseTitle);
55+
collectTitlesPointingTo(baseTitle,0);
5656
}
5757
return $tw.utils.pushTop(titlesPointingFromBase,titlesPointingToBase);
5858
}
5959

6060
/*
61-
Export our filter function
62-
63-
TODO: May I add tests? (editions/test/tiddlers/tests)
64-
*/
61+
Export our filter function
62+
*/
6563
exports.kin = function(source,operator,options) {
6664
var results = [],
6765
needsExclusion = operator.prefix === "!",
6866
suffixes = operator.suffixes || [],
69-
suffixOptions = {
67+
filterOptions = {
68+
wiki: options.wiki,
7069
fieldName: ((suffixes[0] || [])[0] || "tags").toLowerCase(),
7170
direction: ((suffixes[1] || [])[0] || "with").toLowerCase(),
7271
depth: Number((suffixes[2] || [])[0]),
@@ -78,8 +77,8 @@ Finds out where a tiddler originates from and what other tiddlers originate from
7877

7978
if(operator.operand !== "") {
8079
var baseTitle = operator.operand,
81-
baseTiddler = $tw.wiki.getTiddler(baseTitle),
82-
foundTitles = collectTitlesRecursively(baseTiddler,baseTitle,suffixOptions);
80+
baseTiddler = options.wiki.getTiddler(baseTitle),
81+
foundTitles = collectTitlesRecursively(baseTiddler,baseTitle,filterOptions);
8382

8483
source(function(tiddler,title) {
8584
if(needsExclusion !== foundTitles.includes(title)) {
@@ -88,7 +87,7 @@ Finds out where a tiddler originates from and what other tiddlers originate from
8887
});
8988
} else {
9089
source(function(tiddler,title) {
91-
results = $tw.utils.pushTop(results,collectTitlesRecursively(tiddler,title,suffixOptions));
90+
results = $tw.utils.pushTop(results,collectTitlesRecursively(tiddler,title,filterOptions));
9291
});
9392
}
9493

editions/test/tiddlers/tests/test-filters.js

+42
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,48 @@ describe("Filter tests", function() {
203203
expect(wiki.filterTiddlers("[!untagged[]sort[title]]").join(",")).toBe("$:/TiddlerTwo,Tiddler Three,TiddlerOne");
204204
});
205205

206+
describe("testing the kin operator",function() {
207+
// It needs a tree-like wiki to test recursion.
208+
var treeWiki = new $tw.Wiki();
209+
treeWiki.addTiddler({
210+
title: "A",
211+
tags: ["B"],
212+
list: "C"
213+
});
214+
treeWiki.addTiddler({
215+
title: "B",
216+
tags: ["C"],
217+
});
218+
treeWiki.addTiddler({
219+
title: "C",
220+
tags: ["A"],
221+
list: ["E"]
222+
});
223+
treeWiki.addTiddler({
224+
title: "D",
225+
tags: ["B"],
226+
});
227+
treeWiki.addTiddler({
228+
title: "E",
229+
tags: ["D"],
230+
});
231+
treeWiki.addTiddler({
232+
title: "F",
233+
tags: ["E"],
234+
});
235+
treeWiki.addTiddler({
236+
title: "G",
237+
tags: ["D"],
238+
});
239+
240+
it("should handle the kin operator",function() {
241+
expect(treeWiki.filterTiddlers("[kin[A]sort[title]]").join(",")).toBe("A,B,C,D,E,F,G");
242+
expect(treeWiki.filterTiddlers("[kin[A]!kin::to[D]sort[title]]").join(",")).toBe("A,B,C");
243+
expect(treeWiki.filterTiddlers("[kin::from:2[F]sort[title]]").join(",")).toBe("D,E,F");
244+
expect(treeWiki.filterTiddlers("[kin:list[C]]").join(",")).toBe("A,C,E");
245+
});
246+
});
247+
206248
it("should handle the links operator", function() {
207249
expect(wiki.filterTiddlers("[!is[shadow]links[]sort[title]]").join(",")).toBe("$:/TiddlerTwo,a fourth tiddler,one,Tiddler Three,TiddlerSix,TiddlerZero");
208250
expect(wiki.filterTiddlers("[all[shadows]links[]sort[title]]").join(",")).toBe("TiddlerOne");

0 commit comments

Comments
 (0)