parse-features-string.js 712 B

1234567891011121314151617181920
  1. // parses a feature string that has the format used in window.open()
  2. // - `features` input string
  3. // - `emit` function(key, value) - called for each parsed KV
  4. module.exports = function parseFeaturesString (features, emit) {
  5. features = `${features}`
  6. // split the string by ','
  7. features.split(/,\s*/).forEach((feature) => {
  8. // expected form is either a key by itself or a key/value pair in the form of
  9. // 'key=value'
  10. let [key, value] = feature.split(/\s*=/)
  11. if (!key) return
  12. // interpret the value as a boolean, if possible
  13. value = (value === 'yes' || value === '1') ? true : (value === 'no' || value === '0') ? false : value
  14. // emit the parsed pair
  15. emit(key, value)
  16. })
  17. }