forked from moebooru/moebooru
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove prototypejs gsub usage while at it.
- Loading branch information
Showing
1 changed file
with
26 additions
and
22 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
export clamp = (n, min, max) -> | ||
Math.max Math.min(n, max), min | ||
export function clamp (n, min, max) { | ||
return Math.max(Math.min(n, max), min); | ||
} | ||
|
||
# Return the squared distance between two points. | ||
export distanceSquared = (x1, y1, x2, y2) -> | ||
(x1 - x2) ** 2 + (y1 - y2) ** 2 | ||
// Return the squared distance between two points. | ||
export function distanceSquared (x1, y1, x2, y2) { | ||
return (x1 - x2) ** 2 + (y1 - y2) ** 2; | ||
} | ||
|
||
export numberToHumanSize = (size, precision) -> | ||
precision ?= 1 | ||
text = undefined | ||
size = Number(size) | ||
if size.toFixed(0) == '1' | ||
text = '1 Byte' | ||
else if size < 1024 | ||
text = size.toFixed(0) + ' Bytes' | ||
else if size < 1024 * 1024 | ||
text = (size / 1024).toFixed(precision) + ' KB' | ||
else if size < 1024 * 1024 * 1024 | ||
text = (size / (1024 * 1024)).toFixed(precision) + ' MB' | ||
else if size < 1024 * 1024 * 1024 * 1024 | ||
text = (size / (1024 * 1024 * 1024)).toFixed(precision) + ' GB' | ||
else | ||
text = (size / (1024 * 1024 * 1024 * 1024)).toFixed(precision) + ' TB' | ||
export function numberToHumanSize (size, precision) { | ||
precision ??= 1; | ||
size = Number(size); | ||
let text; | ||
if (size.toFixed(0) === '1') { | ||
text = '1 Byte'; | ||
} else if (size < 1024) { | ||
text = `${size.toFixed(0)} Bytes`; | ||
} else if (size < 1024 * 1024) { | ||
text = `${(size / 1024).toFixed(precision)} KB`; | ||
} else if (size < 1024 * 1024 * 1024) { | ||
text = `${(size / (1024 * 1024)).toFixed(precision)} MB`; | ||
} else if (size < 1024 * 1024 * 1024 * 1024) { | ||
text = `${(size / (1024 * 1024 * 1024)).toFixed(precision)} GB`; | ||
} else { | ||
text = `${(size / (1024 * 1024 * 1024 * 1024)).toFixed(precision)} TB`; | ||
} | ||
|
||
text.gsub(/([0-9]\.\d*?)0+ /, '#{1} ').gsub(/\. /, ' ') | ||
return text.replace(/([0-9]\.\d*?)0+ /, '$1 ').replace('. ', ' '); | ||
} |