JSPM

observ-confined

0.0.5
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q35818F
  • License ISC

An observable where the value is checked against a function and changed only if it's passed

Package Exports

  • observ-confined

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (observ-confined) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Usage

var confined = require('observ-confined')
var opts = {minimumValue: -5, maximumValue: 25, initialValue: 10}

function restricter(currentValue, previousValue, options){
  currentValue = Number(currentValue)
  if ( isNaN(currentValue) ) return previousValue
  return currentValue > options.maximumValue ? options.maximumValue
       : currentValue < options.minimumValue ? options.minimumValue
       : /* otherwise */                     : currentValue


}

var boundedValue = confined(restricter)

boundedValue() // returns 10
boundedValue.set(-555) 
boundedValue() // returns -5
boundedValue.set(119) 
boundedValue() // returns 25
boundedValue.set(-3.3) 
boundedValue() // returns -3.3
boundedValue.set(17) 
boundedValue() // returns 17
boundedValue.set('somenotnumericvalue') 
boundedValue() // returns previous value: 17

notes

options are not really required, but I find it really useful to have some way to share values with the outside in both directions

internally nothing expects numbers, it's just the main usecase for me right now