Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

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

### Changed

- Correction of various typos in rules documentations
Expand Down
10 changes: 5 additions & 5 deletions RULES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Rules

- [Rules](#rules)
- [Rules support matrix by techno](#rules-support-matrix-by-techno)
- [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified)
- [Deprecated rules](#deprecated-rules)
- [Refused / Deleted rules](#refused--deleted-rules)
- [Rules support matrix by techno](#rules-support-matrix-by-techno)
- [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified)
- [Deprecated rules](#deprecated-rules)
- [Refused / Deleted rules](#refused--deleted-rules)

## Rules support matrix by techno

Expand Down Expand Up @@ -83,6 +83,7 @@ Some are applicable for different technologies.
| | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚀 |
| | 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) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
| | 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) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 |
| 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. | | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |

## Rules to be reworked / measured / clarified

Expand All @@ -109,7 +110,6 @@ will be completely deleted in next releases and moved to bottom deleted rules ar
|----------|------------|-----------------------------------|-----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 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) |


## Refused / Deleted rules

This table lists rules proposed by the community but refused or/and deleted in creedengo plugins with the justification.
Expand Down
25 changes: 25 additions & 0 deletions src/main/rules/GCI535/GCI535.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"title": "Using external libraries for number format should be avoided in favor or Intl.NumberFormat",
"type": "CODE_SMELL",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "EFFICIENT"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "15min"
},
"tags": [
"creedengo",
"eco-design",
"performance"
],
"defaultSeverity": "Major",
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
56 changes: 56 additions & 0 deletions src/main/rules/GCI535/javascript/GCI535.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
:!sectids:

== Why is this an issue?

Importing an external library for lightweight operations increases overall size of the program.
Using native methods instead reduces the amount of memory and storage to run and store the application.
This is especially critical in environments with limited resources, such as on mobile devices or in web applications
where bandwidth and download times matter.

Smaller programs generally have better runtime performance.
Reducing the number of unnecessary modules minimizes the amount of code that needs to be interpreted or compiled,
leading to faster execution and improved overall performance.

== Examples

*Example with the https://numbrojs.com/[numbro] library, when you use `format` method.*

[source,js]
----
// Example with numbro (not compliant)
import numbro from "numbro";

numbro.setLanguage('en-GB');
var string = numbro(1000).format({
thousandSeparated: true,
}); // '1,000'

// Example with numerable (not compliant)
import { format } from "numerable";
format(1000, '0,0');

// Example with Intl (compliant)
new Intl.NumberFormat("en-GB").format(1000); // '1,000'
----

== Limitations

As for now, only two libraries are handled by this rule :

- https://numbrojs.com/[numbro]
- https://numerablejs.com/lander[numerable]


Some candidates for the future developments are :

- https://github.com/Mottie/javascript-number-formatter[javascript-number-formatter]
- https://www.npmjs.com/package/numerable[numeraljs]
- https://formatjs.github.io/[formatjs]

It’s more likely this rule won’t ever be exhaustive.

== Resources

=== Documentation

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat[Mozilla Web Technology for Developers]