Skip to content

Commit 5920f35

Browse files
author
Fabien Bézagu
committed
Add Intl.NumberFormat (GCI535) javascript rule
1 parent ebf9a5a commit 5920f35

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

RULES.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Rules
22

33
- [Rules](#rules)
4-
- [Rules support matrix by techno](#rules-support-matrix-by-techno)
5-
- [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified)
6-
- [Deprecated rules](#deprecated-rules)
7-
- [Refused / Deleted rules](#refused--deleted-rules)
4+
- [Rules support matrix by techno](#rules-support-matrix-by-techno)
5+
- [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified)
6+
- [Deprecated rules](#deprecated-rules)
7+
- [Refused / Deleted rules](#refused--deleted-rules)
88

99
## Rules support matrix by techno
1010

@@ -83,6 +83,7 @@ Some are applicable for different technologies.
8383
| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚀 |
8484
| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
8585
| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 |
86+
| GCI535 | Use native Intl.NumberFormat to format numbers | There's no need to use a library to display formatted numbers in a recent browser. Use Intl.NumberFormat for that use case. | | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
8687

8788
## Rules to be reworked / measured / clarified
8889

@@ -109,7 +110,6 @@ will be completely deleted in next releases and moved to bottom deleted rules ar
109110
|----------|------------|-----------------------------------|-----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
110111
| EC34 | Java / PHP | Using try...catch...finally calls | Implementation is too simple (only detection of presence of "try" statement) AND replaced by `GCI35` rule | Github discussion with measures : [general/java](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/128) / [php](https://github.com/green-code-initiative/creedengo-php/pull/10) |
111112

112-
113113
## Refused / Deleted rules
114114

115115
This table lists rules proposed by the community but refused or/and deleted in creedengo plugins with the justification.

src/main/rules/GCI535/GCI535.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"title": "Using external libraries for number format should be avoided in favor or Intl.NumberFormat",
3+
"type": "CODE_SMELL",
4+
"code": {
5+
"impacts": {
6+
"RELIABILITY": "MEDIUM"
7+
},
8+
"attribute": "EFFICIENT"
9+
},
10+
"status": "ready",
11+
"remediation": {
12+
"func": "Constant\/Issue",
13+
"constantCost": "15min"
14+
},
15+
"tags": [
16+
"creedengo",
17+
"eco-design",
18+
"performance"
19+
],
20+
"defaultSeverity": "Major",
21+
"compatibleLanguages": [
22+
"JAVASCRIPT",
23+
"TYPESCRIPT"
24+
]
25+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
= You should not format number with an external library (`@creedengo/no-imported-number-format-library`)
2+
3+
⚠️ This rule _warns_ in the following configs: ✅ `flat/recommended`, ✅ `recommended`.
4+
5+
== Why is this an issue?
6+
7+
Importing an external library for lightweight operations increases overall size of the program.
8+
Using native methods instead reduces the amount of memory and storage to run and store the application.
9+
This is especially critical in environments with limited resources, such as on mobile devices or in web applications
10+
where bandwidth and download times matter.
11+
12+
Smaller programs generally have better runtime performance.
13+
Reducing the number of unnecessary modules minimizes the amount of code that needs to be interpreted or compiled,
14+
leading to faster execution and improved overall performance.
15+
16+
== Examples
17+
18+
*Example with the https://numbrojs.com/[numbro] library, when you use `format` method.*
19+
20+
[source,js]
21+
----
22+
// Example with numbro (not compliant)
23+
import numbro from "numbro";
24+
25+
numbro.setLanguage('en-GB');
26+
var string = numbro(1000).format({
27+
thousandSeparated: true,
28+
}); // '1,000'
29+
30+
// Example with numerable (not compliant)
31+
import { format } from "numerable";
32+
format(1000, '0,0');
33+
34+
// Example with Intl (compliant)
35+
new Intl.NumberFormat("en-GB").format(1000); // '1,000'
36+
----
37+
38+
== Limitations
39+
40+
As for now, only two libraries are handled by this rule :
41+
42+
- https://numbrojs.com/[numbro]
43+
- https://numerablejs.com/lander[numerable]
44+
45+
46+
Some candidates for the future developments are :
47+
48+
- https://github.com/Mottie/javascript-number-formatter[javascript-number-formatter]
49+
- https://www.npmjs.com/package/numerable[numeraljs]
50+
- https://formatjs.github.io/[formatjs]
51+
52+
It’s more likely this rule won’t ever be exhaustive.
53+
54+
== Resources
55+
56+
=== Documentation
57+
58+
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat[Mozilla Web Technology for Developers]

0 commit comments

Comments
 (0)