Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Oct 26, 2021
0 parents commit dab5428
Show file tree
Hide file tree
Showing 11 changed files with 2,614 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = tab

[*.yml]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 4 additions & 0 deletions implementation.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
Constrain a value between an upper and lower bound.
*/
export default function clamp(number: number, min: number, max: number): number;
24 changes: 24 additions & 0 deletions implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import esAbstract from 'es-abstract';

// ToNumber() abstract method (https://tc39.es/ecma262/#sec-tonumber) (https://github.com/ljharb/es-abstract/blob/8bc0b761dc26bd7335664ee9eb7095d9fc9d82db/2021/ToNumber.js#L39-L62)
const {ToNumber: toNumber} = esAbstract;

export default function clamp(number, min, max) {
number = toNumber(number);
min = toNumber(min);
max = toNumber(max);

if (min > max) {
throw new RangeError('The minimum value cannot be higher than the maximum value');
}

if (number < min) {
return min;
}

if (number > max) {
return max;
}

return number;
}
Loading

0 comments on commit dab5428

Please sign in to comment.