Как я переопределяю соглашения о присвоении имен направляющих?

Не видел этого. Это довольно кратко и легко понять.

function moneyFormat(price, sign = ' 

Вот версия с большим количеством опций в конечном выводе, позволяющая форматировать разные валюты в разных форматах местности.

// higher order function that takes options then a price and will return the formatted price
const makeMoneyFormatter = ({
  sign = ' 
, delimiter = ',', decimal = '.', append = false, precision = 2, round = true, custom } = {}) => value => { const e = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000] value = round ? (Math.round(value * e[precision]) / e[precision]) : parseFloat(value) const pieces = value .toFixed(precision) .replace('.', decimal) .split('') let ii = pieces.length - (precision ? precision + 1 : 0) while ((ii-=3) > 0) { pieces.splice(ii, 0, delimiter) } if (typeof custom === 'function') { return custom({ sign, float: value, value: pieces.join('') }) } return append ? pieces.join('') + sign : sign + pieces.join('') } // create currency converters with the correct formatting options const formatDollar = makeMoneyFormatter() const formatPound = makeMoneyFormatter({ sign: '£', precision: 0 }) const formatEuro = makeMoneyFormatter({ sign: '€', delimiter: '.', decimal: ',', append: true }) const customFormat = makeMoneyFormatter({ round: false, custom: ({ value, float, sign }) => `SALE:${value}USD` }) console.log( formatPound(1000), formatDollar(10000.0066), formatEuro(100000.001), customFormat(999999.555) )
) { const pieces = parseFloat(price).toFixed(2).split('') let ii = pieces.length - 3 while ((ii-=3) > 0) { pieces.splice(ii, 0, ',') } return sign + pieces.join('') } console.log( moneyFormat(100), moneyFormat(1000), moneyFormat(10000.00), moneyFormat(1000000000000000000) )

Вот версия с большим количеством опций в конечном выводе, позволяющая форматировать разные валюты в разных форматах местности.

// higher order function that takes options then a price and will return the formatted price
const makeMoneyFormatter = ({
  sign = ' 
, delimiter = ',', decimal = '.', append = false, precision = 2, round = true, custom } = {}) => value => { const e = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000] value = round ? (Math.round(value * e[precision]) / e[precision]) : parseFloat(value) const pieces = value .toFixed(precision) .replace('.', decimal) .split('') let ii = pieces.length - (precision ? precision + 1 : 0) while ((ii-=3) > 0) { pieces.splice(ii, 0, delimiter) } if (typeof custom === 'function') { return custom({ sign, float: value, value: pieces.join('') }) } return append ? pieces.join('') + sign : sign + pieces.join('') } // create currency converters with the correct formatting options const formatDollar = makeMoneyFormatter() const formatPound = makeMoneyFormatter({ sign: '£', precision: 0 }) const formatEuro = makeMoneyFormatter({ sign: '€', delimiter: '.', decimal: ',', append: true }) const customFormat = makeMoneyFormatter({ round: false, custom: ({ value, float, sign }) => `SALE:${value}USD` }) console.log( formatPound(1000), formatDollar(10000.0066), formatEuro(100000.001), customFormat(999999.555) )
73
задан Trevor Hickey 21 October 2016 в 23:07
поделиться

2 ответа

Я не эксперт по RoR, но нашел возможный подход . С указанного сайта вы можете добавить правило перегиба в файл config / initializers / inflections.rb :

# Add new inflection rules using the following format 
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'clothing', 'clothes'
end
123
ответ дан 24 November 2019 в 12:16
поделиться

For rails 2.3.2 and maybe 2+, you need to do it a little different:

ActiveSupport::Inflector.inflections do |inflect|
    inflect.plural /^(ox)$/i, '\1\2en'
    inflect.singular /^(ox)en/i, '\1'

    inflect.irregular 'octopus', 'octopi'

    inflect.uncountable "equipment"
end
28
ответ дан 24 November 2019 в 12:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: