lib/cldr-plurals/javascript_emitter.rb in cldr-plurals-1.0.1 vs lib/cldr-plurals/javascript_emitter.rb in cldr-plurals-1.1.0

- old
+ new

@@ -41,12 +41,11 @@ def emit_expression(expr) case expr.value when CldrPlurals::Compiler::Range neg = expr.operation.symbol == '!=' ? '!' : '' - operand_str = emit(expr.operand) - "#{neg}((#{operand_str} >= #{expr.value.start}) && (#{operand_str} <= #{expr.value.finish}))" + "#{neg}(#{emit_range_check(expr.value, expr.operand)})" when CldrPlurals::Compiler::ValueSet "(#{emit_value_set(expr.value, expr.operand, expr.operation)})" else emit_all(expr.operand, expr.operation, expr.value).join(' ') end @@ -55,11 +54,11 @@ def emit_relation(rel) case rel.value when CldrPlurals::Compiler::Range expr = emit(rel.expression) neg = rel.operation.symbol == '!=' ? '!' : '' - "#{neg}((#{expr} >= #{rel.value.start}) && (#{expr} <= #{rel.value.finish}))" + "#{neg}(#{emit_range_check(rel.value, expr)})" when CldrPlurals::Compiler::ValueSet "(#{emit_value_set(rel.value, rel.expression, rel.operation)})" else emit_all(rel.expression, rel.operation, rel.value).join(' ') end @@ -68,21 +67,26 @@ def emit_value_set(value_set, operand, operator) values = value_set.values.map do |value| case value when CldrPlurals::Compiler::Range neg = operator.symbol == '!=' ? '!' : '' - operand_str = emit(operand) - "#{neg}((#{operand_str} >= #{value.start}) && (#{operand_str} <= #{value.finish}))" + "#{neg}(#{emit_range_check(value, operand)})" else "(#{emit(operand)} #{emit(operator)} #{emit(value)})" end end if operator.symbol == '!=' values.join(' && ') else values.join(' || ') end + end + + def emit_range_check(range, operand) + # a..b represents all *integers* between a and b, inclusive. + n = emit(operand) + "(Math.floor(#{n}) === #{n}) && (#{n} >= #{range.start}) && (#{n} <= #{range.finish})" end def emit_operator(op) case op.symbol when '='