Make butlast[0] consistent with rest (#6483)

butlast[0] now returns the input list unchanged.

Fixes: #5833
This commit is contained in:
Ben Webber 2022-02-26 09:42:24 +00:00 committed by GitHub
parent a51191d334
commit 2c38c8351b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 8 deletions

View File

@ -87,7 +87,8 @@ exports.butlast = function(source,operator,options) {
source(function(tiddler,title) {
results.push(title);
});
return results.slice(0,-count);
var index = count === 0 ? results.length : -count;
return results.slice(0,index);
};
exports.bl = exports.butlast;

View File

@ -371,6 +371,7 @@ Tests the filtering mechanism.
expect(wiki.filterTiddlers("[sort[title]reverse[]]").join(",")).toBe("TiddlerOne,Tiddler Three,one,hasList,has filter,filter regexp test,a fourth tiddler,$:/TiddlerTwo,$:/ShadowPlugin");
expect(wiki.filterTiddlers("[sort[title]reverse[x]]").join(",")).toBe("TiddlerOne,Tiddler Three,one,hasList,has filter,filter regexp test,a fourth tiddler,$:/TiddlerTwo,$:/ShadowPlugin");
expect(wiki.filterTiddlers("[sort[title]butlast[]]").join(",")).toBe("$:/ShadowPlugin,$:/TiddlerTwo,a fourth tiddler,filter regexp test,has filter,hasList,one,Tiddler Three");
expect(wiki.filterTiddlers("[sort[title]butlast[0]]").join(",")).toBe("$:/ShadowPlugin,$:/TiddlerTwo,a fourth tiddler,filter regexp test,has filter,hasList,one,Tiddler Three,TiddlerOne");
expect(wiki.filterTiddlers("[sort[title]butlast[2]]").join(",")).toBe("$:/ShadowPlugin,$:/TiddlerTwo,a fourth tiddler,filter regexp test,has filter,hasList,one");
expect(wiki.filterTiddlers("[sort[title]butlast[11]]").join(",")).toBe("");
expect(wiki.filterTiddlers("[sort[title]butlast[x]]").join(",")).toBe("$:/ShadowPlugin,$:/TiddlerTwo,a fourth tiddler,filter regexp test,has filter,hasList,one,Tiddler Three");

View File

@ -1,5 +1,5 @@
created: 20140410103123179
modified: 20150203191930000
modified: 20220226043344258
tags: [[Filter Operators]] [[Order Operators]]
title: butlast Operator
type: text/vnd.tiddlywiki
@ -10,4 +10,6 @@ op-parameter: an integer, defaulting to 1
op-parameter-name: N
op-output: all but the last <<.place N>> input titles
<<.from-version "5.2.2">> The <<.op butlast>> operator returns the input list unchanged if <<.place N>> is 0. This is consistent with the behaviour of the [[rest Operator]].
<<.operator-examples "butlast">>

View File

@ -1,5 +1,5 @@
created: 20150118134611000
modified: 20150118183143000
modified: 20220226043344258
tags: [[butlast Operator]] [[Operator Examples]]
title: butlast Operator (Examples)
type: text/vnd.tiddlywiki
@ -7,5 +7,6 @@ type: text/vnd.tiddlywiki
<<.using-days-of-week>>
<<.operator-example 1 "[list[Days of the Week]butlast[]]">>
<<.operator-example 2 "[list[Days of the Week]butlast[2]]">>
<<.operator-example 3 "A B C D E F G H I J K L M +[butlast[7]]">>
<<.operator-example 2 "[list[Days of the Week]butlast[0]]">>
<<.operator-example 3 "[list[Days of the Week]butlast[2]]">>
<<.operator-example 4 "A B C D E F G H I J K L M +[butlast[7]]">>

View File

@ -1,5 +1,5 @@
created: 20150118134611000
modified: 20150123211722000
modified: 20220226043344258
tags: [[rest Operator]] [[Operator Examples]]
title: rest Operator (Examples)
type: text/vnd.tiddlywiki
@ -7,5 +7,6 @@ type: text/vnd.tiddlywiki
<<.using-days-of-week>>
<<.operator-example 1 "[list[Days of the Week]rest[]]">>
<<.operator-example 2 "[list[Days of the Week]rest[3]]">>
<<.operator-example 3 "Z Y X W V U T S R Q P O +[rest[5]]">>
<<.operator-example 2 "[list[Days of the Week]rest[0]]">>
<<.operator-example 3 "[list[Days of the Week]rest[3]]">>
<<.operator-example 4 "Z Y X W V U T S R Q P O +[rest[5]]">>