This repository was archived by the owner on May 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Number formater #34
Open
Liador
wants to merge
2
commits into
Kalaxia:develop
Choose a base branch
from
Liador:number-formater
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Number formater #34
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||
| const shortVersionTime = 2; | ||||||
| const longVersionTime = 3; | ||||||
| const detailedVersionTime = 10; | ||||||
| const shortNumber = 3; | ||||||
| const detailedNumber = 9; | ||||||
| const decimalSize = 3; | ||||||
| const numberShortNames = [," K"," M"," G"," P"]; | ||||||
| const shortDate = 2; | ||||||
| const longDate = 5; | ||||||
| const dateDivisions = { name:[], value: []}; | ||||||
|
|
||||||
| const fillDateDivisions = () => { | ||||||
| const names = ["week","day","hour","minute","second","millisecond"]; | ||||||
| const values = [7*24*3600*1000,24*3600*1000,3600*1000,60000,1000,1]; | ||||||
| for (const cpt in names) { | ||||||
| dateDivisions.name.push(names[cpt]); | ||||||
| dateDivisions.value.push(values[cpt]); | ||||||
| } | ||||||
| } | ||||||
| export const formatNumber = (number) => { | ||||||
| const values = { short: [], long: [], detailed: []}; | ||||||
| let newNumber; | ||||||
| let decimalNumber; | ||||||
| let numberLength; | ||||||
| if (isNumber(number)){ | ||||||
| decimalNumber=0; | ||||||
| if (number.toString().includes(".")){ | ||||||
| newNumber=number*10; | ||||||
| decimalNumber++; | ||||||
| while (newNumber.toString().includes(".")){ | ||||||
| newNumber = newNumber *10; | ||||||
| decimalNumber ++; | ||||||
| } | ||||||
| if(decimalNumber <= decimalSize) { | ||||||
| numberLength = newNumber.toString().length-decimalNumber; | ||||||
| } | ||||||
| else { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| newNumber=Math.round(newNumber/Math.pow(10,decimalNumber-decimalSize));//-1 pour avoir un nombre avec 1 chiffre apres la virgule et arrondir correctement | ||||||
| decimalNumber=decimalSize; | ||||||
| numberLength= newNumber.toString().length-decimalNumber; | ||||||
| } | ||||||
| } | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pareil ici |
||||||
| else { | ||||||
| newNumber = number; | ||||||
| decimalNumber = 0; | ||||||
| numberLength = newNumber.toString().length; | ||||||
| } | ||||||
|
|
||||||
| (numberLength>shortNumber)? ( | ||||||
| ((numberLength%shortNumber) === 0)? | ||||||
| values.short.push(Math.floor(newNumber/Math.pow(10,numberLength-shortNumber+decimalNumber)) + numberShortNames[(numberLength/3)-1]): | ||||||
| values.short.push(Math.round(newNumber/Math.pow(10,(numberLength+decimalNumber-shortNumber)))/Math.pow(10,shortNumber-numberLength%shortNumber)+numberShortNames[Math.floor((numberLength)/3)]) | ||||||
| ) : values.short.push(Math.round(newNumber/Math.pow(10,-shortNumber+numberLength+decimalNumber))/Math.pow(10,shortNumber-numberLength)); | ||||||
|
|
||||||
| let detailedFormat=""; | ||||||
| let cpt = 0; | ||||||
| let numberTemp = newNumber/Math.pow(10,decimalNumber); | ||||||
| let stringTemp = numberTemp.toString(); | ||||||
| if (decimalNumber!=0) | ||||||
| while (cpt<= decimalNumber){ | ||||||
| cpt++; | ||||||
| detailedFormat=stringTemp[stringTemp.length-cpt]+detailedFormat; | ||||||
| } | ||||||
| numberTemp=Math.floor(numberTemp); | ||||||
|
|
||||||
| cpt = 0; | ||||||
| while (cpt<numberLength) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remonte l'accolage d'une ligne |
||||||
| { | ||||||
| detailedFormat=numberTemp%1000 + ((cpt==0)?"":" ") +detailedFormat; | ||||||
| numberTemp=(numberTemp-(numberTemp%1000))/1000; | ||||||
| cpt=cpt+3; | ||||||
| } | ||||||
| values.detailed.push(detailedFormat); | ||||||
| } | ||||||
| return values; | ||||||
| }; | ||||||
|
|
||||||
| const isNumber = (n) => { | ||||||
| return /^-?[\d.]+(?:e-?\d+)?$/.test(n); | ||||||
| }; | ||||||
|
|
||||||
| export const formatDate = (datetime) => { | ||||||
| const dateInput= document.getElementById("date"); | ||||||
| const date = dateInput.value; | ||||||
| fillDateDivisions(); | ||||||
| let now= new Date(); | ||||||
| if (Math.abs(now - datetime)>1000) { | ||||||
| if (datetime>now) { | ||||||
| return "in " +formatTime(datetime-now, shortDate); | ||||||
| } | ||||||
| else { | ||||||
| return formatTime(now-datetime,1)+" ago"; | ||||||
| } | ||||||
| } | ||||||
| else { | ||||||
| return "Now"; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const formatTime = (duration, size) => { | ||||||
| let stringDuration = ""; | ||||||
| let cpt = 0; | ||||||
| let stringSize =0; | ||||||
| let currentNumberOfTimeUnit; | ||||||
| while( size>stringSize) { | ||||||
| currentNumberOfTimeUnit =Math.floor(duration/dateDivisions.value[cpt]); | ||||||
| stringDuration = stringDuration+ | ||||||
| ((currentNumberOfTimeUnit>0)? | ||||||
| (currentNumberOfTimeUnit)+" "+dateDivisions.name[cpt]+ | ||||||
| ((currentNumberOfTimeUnit>1)?"s " | ||||||
| :" ") | ||||||
| :"") | ||||||
| ; | ||||||
| (Math.floor(currentNumberOfTimeUnit)>0||stringSize>0)?stringSize++:cpt=cpt; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usine à gaz de la mort sur ces lignes, aère ton code :) |
||||||
| duration=duration-(currentNumberOfTimeUnit)*dateDivisions.value[cpt]; | ||||||
| cpt++; | ||||||
| } | ||||||
| return stringDuration; | ||||||
| } | ||||||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.