Module:Age: Difference between revisions

    (update from sandbox with features discussed at Template talk:Age)
    (update from sandbox: can spell ages using age_generic with format=cardinal + format=ordinal and more)
    Line 99: Line 99:
    end
    end
    return groups:join(',')
    return groups:join(',')
    end
    local function spellNumber(number, options, i)
    -- Return result of spelling number, or
    -- return number (as a string) if cannot spell it.
    -- i == 1 for the first number which can optionally start with an uppercase letter.
    number = tostring(number)
    return require('Module:ConvertNumeric').spell_number(
    number,
    nil,                      -- fraction numerator
    nil,                      -- fraction denominator
    i == 1 and options.upper,  -- true: 'One' instead of 'one'
    not options.us,            -- true: use 'and' between tens/ones etc
    options.adj,              -- true: hyphenated
    options.ordinal            -- true: 'first' instead of 'one'
    ) or number
    end
    end


    Line 190: Line 206:
    debug = 'sortable_debug',
    debug = 'sortable_debug',
    },
    },
    }
    local spellOptions = {
    cardinal = {},
    Cardinal = { upper = true },
    cardinal_us = { us = true },
    Cardinal_us = { us = true, upper = true },
    ordinal = { ordinal = true },
    Ordinal = { ordinal = true, upper = true },
    ordinal_us = { ordinal = true, us = true },
    Ordinal_us = { ordinal = true, us = true, upper = true },
    }
    }


    Line 247: Line 274:
    end
    end


    local function makeText(values, components, names, options)
    local function makeText(values, components, names, options, noUpper)
    -- Return wikitext representing an age or duration.
    -- Return wikitext representing an age or duration.
    local text = Collection.new()
    local text = Collection.new()
    Line 257: Line 284:
    if (islist or v > 0) or (text.n == 0 and i == count) or (text.n > 0 and components.keepZero) then
    if (islist or v > 0) or (text.n == 0 and i == count) or (text.n > 0 and components.keepZero) then
    local fmt, vstr
    local fmt, vstr
    if i == 1 and options.format == 'format_commas' then
    if options.spell then
    fmt = function(number)
    return spellNumber(number, options.spell, noUpper or i)
    end
    elseif i == 1 and options.format == 'format_commas' then
    -- Numbers after the first should be small and not need formatting.
    -- Numbers after the first should be small and not need formatting.
    fmt = formatNumber
    fmt = formatNumber
    Line 264: Line 295:
    end
    end
    if islist then
    if islist then
    vstr = fmt(v[1]) .. rangeJoin(options.range) .. fmt(v[2])
    vstr = fmt(v[1]) .. rangeJoin(options.range)
    noUpper = true
    vstr = vstr .. fmt(v[2])
    else
    else
    vstr = fmt(v)
    vstr = fmt(v)
    Line 407: Line 440:
    isnegative = diff.isnegative,
    isnegative = diff.isnegative,
    range = parms.range,
    range = parms.range,
    spell = parms.spell,
    }
    }
    if show.id == 'hide' then
    if show.id == 'hide' then
    Line 422: Line 456:
    join = textOptions.join,
    join = textOptions.join,
    isnegative = textOptions.isnegative,
    isnegative = textOptions.isnegative,
    spell = textOptions.spell,
    }
    }
    return
    return
    Line 427: Line 462:
    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) ..
    makeText({ diff.partial.maxdiff:age(show.id, diffOptions) }, show, names[abbr], opt) ..
    makeText({ diff.partial.maxdiff:age(show.id, diffOptions) }, show, names[abbr], opt, true) ..
    (textOptions.suffix or '')
    (textOptions.suffix or '')
    end
    end
    Line 684: Line 719:
    end
    end
    local format = stripToNil(args.format)
    local format = stripToNil(args.format)
    local spell = spellOptions[format]
    if format then
    if format then
    format = 'format_' .. format
    format = 'format_' .. format
    Line 702: Line 738:
    sep = spec.sep,
    sep = spec.sep,
    sortable = translateParameters.sortable[args.sortable or spec.sortable],
    sortable = translateParameters.sortable[args.sortable or spec.sortable],
    spell = spell,
    }
    }
    if (spec.negative or frame.args.negative) == 'error' and parms.diff.isnegative then
    if (spec.negative or frame.args.negative) == 'error' and parms.diff.isnegative then