Skip to content

Commit a7eefd3

Browse files
fbezaguFabien Bézagu
andauthored
Add Intl.NumberFormat (GCI535) javascript rule (#400)
Co-authored-by: Fabien Bézagu <[email protected]>
1 parent ec76ad5 commit a7eefd3

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- [#400](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/400) Add rule GCI535 - Prefer usage of Intl.NumberFormat
13+
1214
### Changed
1315

1416
### Deleted

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

@@ -84,6 +84,7 @@ Some are applicable for different technologies.
8484
| | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚀 |
8585
| | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
8686
| | 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) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 |
87+
| 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. | | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
8788

8889

8990

@@ -112,7 +113,6 @@ will be completely deleted in next releases and moved to bottom deleted rules ar
112113
|----------|------------|-----------------------------------|-----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
113114
| 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) |
114115

115-
116116
## Refused / Deleted rules
117117

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

0 commit comments

Comments
 (0)