jupyter-ijavascript-utils 1.6.7 → 1.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/format.js +31 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jupyter-ijavascript-utils",
3
- "version": "1.6.7",
3
+ "version": "1.6.8",
4
4
  "description": "Utilities for working with iJavaScript - a Jupyter Kernel",
5
5
  "homepage": "https://jupyter-ijavascript-utils.onrender.com/",
6
6
  "license": "MIT",
package/src/format.js CHANGED
@@ -321,6 +321,7 @@ module.exports.ellipsify = function ellipsify(str, maxLen = 50) {
321
321
  * @param {Array} range.rangeMin - minimum output value
322
322
  * @param {Array} range.rangeMax - maximum output value
323
323
  * @returns Number
324
+ * @see {@link module:format.clampDomain|clampDomain(value, [min, max])}
324
325
  * @example
325
326
  *
326
327
  * format.mapDomain(-2, [0, 10], [0, 1])
@@ -333,8 +334,8 @@ module.exports.ellipsify = function ellipsify(str, maxLen = 50) {
333
334
  * // 1 - since it is above the maximum value
334
335
  *
335
336
  * format.mapDomain(0.5, [0, 1], [0, 10])
336
- * // 5 - since it is half of 0-1, and half of 1-10
337
337
  * format.mapDomain(0.5, [0, 1], [0, Math.PI + Math.PI])
338
+ * // 5 - since it is half of 0-1, and half of 1-10
338
339
  * // 3.1415 or Math.PI - since it is half of 2 PI
339
340
  */
340
341
  module.exports.mapDomain = function mapDomain(val, [domainMin, domainMax], [rangeMin = 0, rangeMax = 1]) {
@@ -424,3 +425,32 @@ module.exports.timePeriod = function mapTime(millisecondPeriod, timeMilli = null
424
425
  module.exports.timePeriodPercent = function mapEpochInPeriod(millisecondPeriod, timeEpoch = new Date().getTime()) {
425
426
  return (timeEpoch % millisecondPeriod) / millisecondPeriod;
426
427
  };
428
+
429
+ /**
430
+ * Clamps (restircts) a value to a specific domain.
431
+ *
432
+ * Meaning if value is less than minimum, then the minimum is returned.
433
+ * If the value is greater than the maximum, then the maximum is returned.
434
+ *
435
+ * NOTE: null or undefined are not treated specially when comparing to maximum or minimum values.
436
+ *
437
+ * @param {Number} value - the value that will be modified if less than min or max
438
+ * @param {Array} domain - Domain of min and max values
439
+ * @param {Number} domain.min - the minimum value allowable
440
+ * @param {Number} domain.max - the maximum value allowable
441
+ * @returns {Number} - minimum if value is less than minimum, maximum if more, value otherwise.
442
+ *
443
+ * @see {@link module:format.mapDomain|mapDomain(value, [min, max], [newMin, newMax])}
444
+ * @example
445
+ * format.clampDomain( -1, [0, 1]); // 0
446
+ * format.clampDomain( 2, [0, 1]); // 1
447
+ * format.clampDomain( 0.5, [0, 1]); // 0.5
448
+ **/
449
+ module.exports.clampDomain = function clampDomain(value, [minimum, maximum]) {
450
+ if (value < minimum) {
451
+ return minimum;
452
+ } else if (value > maximum) {
453
+ return maximum;
454
+ }
455
+ return value;
456
+ };