Jump to content
Wikipedia The Free Encyclopedia

Module:Sortkey/sandbox

From Wikipedia, the free encyclopedia
This is the module sandbox page for Module:Sortkey (diff).
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
This module depends on the following other modules:

This module sanitizes and escapes sortkeys so that they can be used inside the data-sort-value attribute of HTML tags in combination with table sorting. Use it when you cannot be certain that the provided sortkey input is a correct sortkey.

Usage

[edit ]

{{#invoke:Sortkey|escape|sortkey to sanitize}}

The above documentation is transcluded from Module:Sortkey/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
 localgetArgs=require('Module:Arguments').getArgs

 localp={}

 functionp._encode(sortkey)
 -- Protect against sortkey nesting.
 -- Example: {{sort|{{dts|2013|07|07}}|{{dts|1990|12|01}}}}
 ifstring.find(sortkey,"sortkey")orstring.find(sortkey,"data-sort-value")then
 return"";
 end
 returnmw.text.encode(sortkey)
 end

 functionp.encode(frame)
 localargs=getArgs(frame);
 returnp._encode(args[1]or"")
 end

 localfunctionvalid_number(num)
 -- Return true if num is a valid number.
 -- In Scribunto (different from some standard Lua), when expressed as a string,
 -- overflow or other problems are indicated with text like "inf" or "nan"
 -- which are regarded as invalid here (each contains "n").
 iftype(num)=='number'andtostring(num):find('n',1,true)==nilthen
 returntrue
 end
 end

 functionp._sortKeyForNumber(value)
 localsortkey
 ifnotvalid_number(value)then
 ifvalue<0then
 sortkey='1000000000000000000'
 else
 sortkey='9000000000000000000'
 end
 elseifvalue==0then
 sortkey='5000000000000000000'
 else
 localmag=math.floor(math.log10(math.abs(value))+1e-14)
 localprefix
 ifvalue>0then
 prefix=7000+mag
 else
 prefix=2999-mag
 value=value+10^(mag+1)
 end
 sortkey=string.format('%d',prefix)..string.format('%015.0f',math.floor(value*10^(math.min(28,14-mag))))
 end
 returnsortkey;
 end

 functionp.sortKeyForNumber(frame)
 localargs=getArgs(frame)
 returnp._sortKeyForNumber(args[1]or"")
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /