Module:Age: Difference between revisions

    (update from sandbox: can spell ages using age_generic with format=cardinal + format=ordinal and more)
    (update from sandbox: prefix=TEXT inserts TEXT before visible result but after any sort key, with space appended to TEXT if it is more than one character)
    Line 115: Line 115:
    options.ordinal            -- true: 'first' instead of 'one'
    options.ordinal            -- true: 'first' instead of 'one'
    ) or number
    ) or number
    end
    local function makeExtra(args, flagCurrent)
    -- Return extra text that will be inserted before the visible result
    -- but after any sort key.
    local extra = args.prefix or ''
    if mw.ustring.len(extra) > 1 then
    -- Parameter "~" gives "~3" whereas "over" gives "over 3".
    extra = extra .. ' '
    end
    if flagCurrent then
    extra = '<span class="currentage"></span>' .. extra
    end
    return extra
    end
    end


    Line 121: Line 135:
    -- Assume value is a valid number which has not overflowed.
    -- Assume value is a valid number which has not overflowed.
    if sortable == 'sortable_table' or sortable == 'sortable_on' or sortable == 'sortable_debug' then
    if sortable == 'sortable_table' or sortable == 'sortable_on' or sortable == 'sortable_debug' then
    local sortkey
    local sortKey
    if value == 0 then
    if value == 0 then
    sortkey = '5000000000000000000'
    sortKey = '5000000000000000000'
    else
    else
    local mag = math.floor(math.log10(math.abs(value)) + 1e-14)
    local mag = math.floor(math.log10(math.abs(value)) + 1e-14)
    local prefix
    if value > 0 then
    if value > 0 then
    prefix = 7000 + mag
    sortKey = 7000 + mag
    else
    else
    prefix = 2999 - mag
    sortKey = 2999 - mag
    value = value + 10^(mag+1)
    value = value + 10^(mag+1)
    end
    end
    sortkey = string.format('%d', prefix) .. string.format('%015.0f', math.floor(value * 10^(14-mag)))
    sortKey = string.format('%d', sortKey) .. string.format('%015.0f', math.floor(value * 10^(14-mag)))
    end
    end
    local lhs, rhs
    local lhs, rhs
    Line 145: Line 158:
    rhs = '♠</span>'
    rhs = '♠</span>'
    end
    end
    return lhs .. sortkey .. rhs
    return lhs .. sortKey .. rhs
    end
    end
    end
    end
    Line 250: Line 263:
    end
    end
    end
    end
    local prefix, result
    local sortKey, result
    local sortable = translateParameters.sortable[args.sortable]
    local sortable = translateParameters.sortable[args.sortable]
    if sortable then
    if sortable then
    local value = (date.partial and date.partial.first or date).jdz
    local value = (date.partial and date.partial.first or date).jdz
    prefix = makeSort(value, sortable)
    sortKey = makeSort(value, sortable)
    end
    end
    if show ~= 'hide' then
    if show ~= 'hide' then
    Line 266: Line 279:
    end
    end
    end
    end
    return (prefix or '') .. (result or '')
    return (sortKey or '') .. (result or '')
    end
    end


    Line 344: Line 357:
    end
    end
    return
    return
    (options.prefix or '') ..
    (options.sortKey or '') ..
    (options.extra or '') ..
    (options.extra or '') ..
    sign ..
    sign ..
    Line 424: Line 437:
    range = parms.range and true or nil,
    range = parms.range and true or nil,
    }
    }
    local prefix
    local sortKey
    if parms.sortable then
    if parms.sortable then
    local value = diff.age_days + (parms.wantDuration and 1 or 0)  -- days and fraction of a day
    local value = diff.age_days + (parms.wantDuration and 1 or 0)  -- days and fraction of a day
    Line 430: Line 443:
    value = -value
    value = -value
    end
    end
    prefix = makeSort(value, parms.sortable)
    sortKey = makeSort(value, parms.sortable)
    end
    end
    local textOptions = {
    local textOptions = {
    prefix = prefix,
    suffix = parms.suffix,  -- not currently used
    extra = parms.extra,
    extra = parms.extra,
    format = parms.format,
    format = parms.format,
    Line 440: Line 451:
    isnegative = diff.isnegative,
    isnegative = diff.isnegative,
    range = parms.range,
    range = parms.range,
    sortKey = sortKey,
    spell = parms.spell,
    spell = parms.spell,
    suffix = parms.suffix,  -- not currently used
    }
    }
    if show.id == 'hide' then
    if show.id == 'hide' then
    return prefix or ''
    return sortKey or ''
    end
    end
    local values = { diff:age(show.id, diffOptions) }
    local values = { diff:age(show.id, diffOptions) }
    Line 459: Line 472:
    }
    }
    return
    return
    (textOptions.prefix or '') ..
    (textOptions.sortKey or '') ..
    makeText({ diff.partial.mindiff:age(show.id, diffOptions) }, show, names[abbr], opt) ..
    makeText({ diff.partial.mindiff:age(show.id, diffOptions) }, show, names[abbr], opt) ..
    rangeJoin(textOptions.range) ..
    rangeJoin(textOptions.range) ..
    Line 733: Line 746:
    abbr = spec.abbr,
    abbr = spec.abbr,
    disp = spec.disp,
    disp = spec.disp,
    extra = (getopt.usesCurrent and format ~= 'format_raw') and '<span class="currentage"></span>' or nil,
    extra = makeExtra(args, getopt.usesCurrent and format ~= 'format_raw'),
    format = format or spec.format,
    format = format or spec.format,
    round = yes(args.round),
    round = yes(args.round),
    Line 865: Line 878:
    local args = frame:getParent().args
    local args = frame:getParent().args
    local parms = {
    local parms = {
    extra = makeExtra(args),
    wantDuration = yes(args.duration),
    wantDuration = yes(args.duration),
    range = yes(args.range) or (args.range == 'dash' and 'dash' or nil),
    range = yes(args.range) or (args.range == 'dash' and 'dash' or nil),