Created
August 5, 2015 01:34
-
-
Save ksin/7bc0ebd65438da6f211c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Numbers | |
WEIRD = { | |
0 => "zero", | |
1 => "one", | |
2 => "two", | |
3 => "three", | |
4 => "four", | |
5 => "five", | |
6 => "six", | |
7 => "seven", | |
8 => "eight", | |
9 => "nine", | |
10 => "ten", | |
11 => "eleven", | |
12 => "twelve", | |
13 => "thirteen", | |
14 => "fourteen", | |
15 => "fifteen", | |
16 => "sixteen" , | |
17 => "seventeen", | |
18 => "eighteen", | |
19 => "nineteen", | |
20 => "twenty", | |
30 => "thirty", | |
40 => "forty", | |
50 => "fifty", | |
60 => "sixty", | |
70 => "seventy", | |
80 => "eighty", | |
90 => "ninety" | |
} | |
TENS = { | |
0 => "one", | |
1 => "ten", | |
2 => "hundred", | |
3 => "thousand", | |
6 => "million", | |
9 => "billion", | |
12 => "trillion", | |
15 => "quadrillion", | |
18 => "quintillion", | |
21 => "sextillion", | |
24 => "septillion", | |
27 => "octillion", | |
30 => "nonillion", | |
33 => "decillion", | |
36 => "undecillion", | |
39 => "duodecillion", | |
42 => "tredecillion", | |
45 => "quattuordecillion", | |
48 => "quindecillion", | |
51 => "sexdecillion", | |
54 => "septendecillion", | |
57 => "octodecillion", | |
60 => "novemdecillion", | |
63 => "vigintillion", | |
66 => "unvigintillion", | |
69 => "duovigintillion", | |
72 => "trevigintillion", | |
75 => "quattuorvigintillion", | |
78 => "quinvigintillion", | |
81 => "sexvigintillion", | |
84 => "septenvigintillion", | |
87 => "octovigintillion", | |
90 => "novemvigintillion", | |
93 => "trigintillion", | |
96 => "untrigintillion", | |
99 => "duotrigintillion", | |
100 => "googol" | |
} | |
def self.in_words(num) | |
return WEIRD[num] if WEIRD[num] | |
power = Math.log10(num).floor | |
# Assign tens_word until not nil | |
until tens_word = TENS[power] | |
power -= 1 | |
end | |
tens_num = 10 ** power | |
div, mod = num.divmod(tens_num) | |
tens_word = WEIRD[div * tens_num] | |
before_part = if tens_word | |
tens_word | |
else | |
self.in_words(div) + ' ' + TENS[power] | |
end | |
after_part = self.in_words(mod) | |
if after_part == "zero" | |
return before_part | |
else | |
return before_part + ' ' + after_part | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ember-template-lint
ember-template-lint will lint your template and return error results. This is commonly
used through ember-cli-template-lint which adds failing lint tests for consuming ember-cli
applications.
For example, given the rule
bare-strings
is enabled, this template would bein violation:
When ran through the linter's
verify
method, we would have a single result indicating thatthe
bare-strings
rule found an error.Install
To install ember-template-lint
Usage
results
will be an array of objects which have the following properties:rule
- The name of the rule that triggered this warning/error.message
- The message that should be output.line
- The line on which the error occurred.column
- The column on which the error occurred.moduleId
- The module path for the file containing the error.source
- The source that caused the error.fix
- An object describing how to fix the error.Configuration
Project Wide
You can turn on specific rules by toggling them in a
.template-lintrc.js
file at the base of your project:This extends from the builtin recommended configuration (lib/config/recommended.js),
and also enables the
bare-strings
rule (see here).Using this mechanism allows you to extend from the builtin, and modify specific rules as needed.
Some rules also allow setting additional configuration, for example if you would like to configure
some "bare strings" that are allowed you might have:
Per Template
It is also possible to disable specific rules (or all rules) in a template itself:
It is not currently possible to change rule configuration in the template.
Configuration Keys
The following properties are allowed in the root of the
.template-lintrc.js
configuration file:rules
-- This is an object containing rule specific configuration (see details for each rule below).extends
-- This is a string that allows you to specify an internally curated list of rules (we suggestrecommended
here).pending
-- An array of module id's that are still pending. The goal of this array is to allow incorporating template lintinginto an existing project, without changing every single template file. You can add all existing templates to this
pending
listingand slowly work through them, while at the same time ensuring that new templates added to the project pass all defined rules.
Rules
bare-strings
In order to be able to internationalize your application, you will need to avoid using plain strings in your templates. Instead, you would need to use a template helper specializing in translation (ember-i18n and ember-intl are great projects to use for this).
This rule forbids the following:
The following values are valid configuration:
true
for enabled /false
for disabledwhitelist
-- An array of whitelisted stringsglobalAttributes
-- An array of attributes to check on every element.elementAttributes
-- An object whose keys are tag names and value is an array of attributes to check for that tag name.When the config value of
true
is used the following configuration is used:whitelist
-(),.&+-=*/#%!?:[]{}
globalAttributes
-title
elementAttributes
-{ img: ['alt'], input: ['placeholder'] }
block-indentation
Good indentation is crucial for long term maintenance of templates. For example, having blocks misaligned is a common cause of logic errors...
This rule forbids the following examples:
The following values are valid configuration:
true
indicates a 2 space indent,false
indicates that the rule is disabled.html-comments
Html comments in your templates will get compiled and rendered into the DOM at runtime. Instead you can annotate your templates using Handlebars comments, which will be stripped out when the template is compiled and have no effect at runtime.
This rule forbids the following:
<!-- comment goes here -->
but allows the following:
{{!-- comment goes here --}}
Html comments containing linting instructions such as:
<!-- template-lint bare-strings=false -->
are of course allowed (and since the linter strips them during processing, they will not get compiled and rendered into the DOM regardless of this rule).
triple-curlies
Usage of triple curly braces to allow raw HTML to be injected into the DOM is large vector for exploits of your application (especially when the raw HTML is user controllable ). Instead of using
{{{foo}}}
, you should use appropriate helpers or computed properties that return aSafeString
(viaEmber.String.htmlSafe
generally) and ensure that user supplied data is properly escaped.This rule forbids the following:
nested-interactive
Usage of nested
interactive content
can lead to UX problems, accessibilityproblems, bugs and in some cases to DOM errors. You should not put interactive
content elements nested inside other interactive content elements. Instead using
nested interactive content elements you should separate them and put them one
after the other.
This rule forbids the following:
The following values are valid configuration:
true
indicates all whitelist test will run,false
indicates that the rule is disabled.ignoredTags
- An array of element tag names that should be whitelisted. Default to[]
.ignoreTabindex
- Whentrue
tabindex will be ignored. Defaults tofalse
.ignoreUsemapAttribute
- Whentrue
ignores theusemap
attribute onimg
andobject
elements. Defaultsfalse
.additionalInteractiveTags
- An array of element tag names that should also be considered as interactive. Defaults to[]
.'self-closing-void-elements
HTML has no self-closing tags. The HTML 5 parser will ignore self-closing tag in
the case of
void elements
(tags that shouldn't have a
closing tag
). Although the parser will ignore it'sunnecessary and can lead to confusing with SVG/XML code.
This rule forbids the following:
Instead, you should write the template as:
The following values are valid configuration:
true
for enabled /false
for disabledimg-alt-attributes
An
<img>
without analt
attribute is essentially invisible to assistive technology (i.e. screen readers).In order to ensure that screen readers can provide useful information, we need to ensure that all
<img>
elementshave an
alt
specified. See WCAG Suggestion H37.The rule forbids the following:
Instead, you should write the template as:
The following values are valid configuration:
true
for enabled /false
for disabledinvalid-interactive
Adding interactivity to an element that is not naturally interactive content leads to a very poor experience for
users of assistive technology (i.e. screen readers). In order to ensure that screen readers can provide useful information
to their users, we should add an appropriate
role
attribute when the underlying element would not have made thatrole obvious.
This rule forbids the following:
Instead, you should add a
role
to the element in question so that the A/T is aware that it is interactive:The following values are valid configuration (same as the
nested-interactive
rule above):true
indicates all whitelist test will run,false
indicates that the rule is disabled.ignoredTags
- An array of element tag names that should be whitelisted. Default to[]
.ignoreTabindex
- Whentrue
tabindex will be ignored. Defaults tofalse
.ignoreUsemapAttribute
- Whentrue
ignores theusemap
attribute onimg
andobject
elements. Defaultsfalse
.additionalInteractiveTags
- An array of element tag names that should also be considered as interactive. Defaults to[]
.'Deprecations
deprecated-each-syntax
In Ember 2.0, support for using the
in
form of the{{#each}}
helperhas been removed.
For example, this rule forbids the following:
Instead, you should write the template as:
More information is available at the Deprecation Guide.
Contributing
A few ideas for where to take this in the future:
can provide feedback to devs during development
Installation
git clone
this repositorynpm install
Running Tests
npm test