module.exports = functionmoney_format (format, number) {
const setlocale = require('../strings/setlocale')
if (typeof number !== 'number') {
returnnull
}
const regex = /%((=.|[+^(!-])*?)(\d*?)(#(\d+))?(\.(\d+))?([in%])/g
setlocale('LC_ALL', 0)
const $global = (typeofwindow !== 'undefined' ? window : global)
$global.$locutus = $global.$locutus || {}
const $locutus = $global.$locutus
$locutus.php = $locutus.php || {}
const monetary = $locutus.php.locales[$locutus.php.localeCategories.LC_MONETARY].LC_MONETARY
const doReplace = function (n0, flags, n2, width, n4, left, n6, right, conversion) {
let value = ''
let repl = ''
if (conversion === '%') {
return'%'
}
const fill = flags && (/=./).test(flags) ? flags.match(/=(.)/)[1] : ' '
const showCurrSymbol = !flags || flags.indexOf('!') === -1
width = parseInt(width, 10) || 0
const neg = number < 0
number = number + ''
number = neg ? number.slice(1) : number
const decpos = number.indexOf('.')
let integer = decpos !== -1 ? number.slice(0, decpos) : number
let fraction = decpos !== -1 ? number.slice(decpos + 1) : ''
const _strSplice = function (integerStr, idx, thouSep) {
const integerArr = integerStr.split('')
integerArr.splice(idx, 0, thouSep)
return integerArr.join('')
}
const intLen = integer.length
left = parseInt(left, 10)
const filler = intLen < left
if (filler) {
var fillnum = left - intLen
integer = newArray(fillnum + 1).join(fill) + integer
}
if (flags.indexOf('^') === -1) {
let thouSep = monetary.mon_thousands_sep
const monGrouping = monetary.mon_grouping
if (monGrouping[0] < integer.length) {
for (var i = 0, idx = integer.length; i < monGrouping.length; i++) {
idx -= monGrouping[i]
if (idx <= 0) {
break
}
if (filler && idx < fillnum) {
thouSep = fill
}
integer = _strSplice(integer, idx, thouSep)
}
}
if (monGrouping[i - 1] > 0) {
while (idx > monGrouping[i - 1]) {
idx -= monGrouping[i - 1]
if (filler && idx < fillnum) {
thouSep = fill
}
integer = _strSplice(integer, idx, thouSep)
}
}
}
if (right === '0') {
value = integer
} else {
let decPt = monetary.mon_decimal_point
if (right === '' || right === undefined) {
right = conversion === 'i' ? monetary.int_frac_digits : monetary.frac_digits
}
right = parseInt(right, 10)
if (right === 0) {
fraction = ''
decPt = ''
} elseif (right < fraction.length) {
fraction = Math.round(parseFloat(
fraction.slice(0, right) + '.' + fraction.substr(right, 1)
))
if (right > fraction.length) {
fraction = newArray(right - fraction.length + 1).join('0') + fraction
}
} elseif (right > fraction.length) {
fraction += newArray(right - fraction.length + 1).join('0')
}
value = integer + decPt + fraction
}
let symbol = ''
if (showCurrSymbol) {
symbol = conversion === 'i' ? monetary.int_curr_symbol : monetary.currency_symbol
}
const signPosn = neg ? monetary.n_sign_posn : monetary.p_sign_posn
const sepBySpace = neg ? monetary.n_sep_by_space : monetary.p_sep_by_space
const csPrecedes = neg ? monetary.n_cs_precedes : monetary.p_cs_precedes
if (flags.indexOf('(') !== -1) {
repl = (csPrecedes ? symbol + (sepBySpace === 1 ? ' ' : '') : '') + value + (!csPrecedes
? (sepBySpace === 1 ? ' ' : '') + symbol
: '')
if (neg) {
repl = '(' + repl + ')'
} else {
repl = ' ' + repl + ' '
}
} else {
const posSign = monetary.positive_sign
const negSign = monetary.negative_sign
const sign = neg ? (negSign) : (posSign)
const otherSign = neg ? (posSign) : (negSign)
let signPadding = ''
if (signPosn) {
signPadding = newArray(otherSign.length - sign.length + 1).join(' ')
}
let valueAndCS = ''
switch (signPosn) {
case0:
valueAndCS = csPrecedes
? symbol + (sepBySpace === 1 ? ' ' : '') + value
: value + (sepBySpace === 1 ? ' ' : '') + symbol
repl = '(' + valueAndCS + ')'
break
case1:
valueAndCS = csPrecedes
? symbol + (sepBySpace === 1 ? ' ' : '') + value
: value + (sepBySpace === 1 ? ' ' : '') + symbol
repl = signPadding + sign + (sepBySpace === 2 ? ' ' : '') + valueAndCS
break
case2:
valueAndCS = csPrecedes
? symbol + (sepBySpace === 1 ? ' ' : '') + value
: value + (sepBySpace === 1 ? ' ' : '') + symbol
repl = valueAndCS + (sepBySpace === 2 ? ' ' : '') + sign + signPadding
break
case3:
repl = csPrecedes
? signPadding + sign + (sepBySpace === 2 ? ' ' : '') + symbol +
(sepBySpace === 1 ? ' ' : '') + value
: value + (sepBySpace === 1 ? ' ' : '') + sign + signPadding +
(sepBySpace === 2 ? ' ' : '') + symbol
break
case4:
repl = csPrecedes
? symbol + (sepBySpace === 2 ? ' ' : '') + signPadding + sign +
(sepBySpace === 1 ? ' ' : '') + value
: value + (sepBySpace === 1 ? ' ' : '') + symbol +
(sepBySpace === 2 ? ' ' : '') + sign + signPadding
break
}
}
let padding = width - repl.length
if (padding > 0) {
padding = newArray(padding + 1).join(' ')
if (flags.indexOf('-') !== -1) {
repl += padding
} else {
repl = padding + repl
}
}
return repl
}
return format.replace(regex, doReplace)
}