parse-features-string.js 747 B

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