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AGd,EAEN,CACT,CACF,CASA,wCAAO,CAAkC,GAGvC,GAAI,EAAW,OAAS,EAEtB,MAAO,aACF,CAIL,MAAM,EAAO,EAAW,GAGxB,IAAI,EAEJ,MAAM,EAHW,gDAGM,KAAK,GAS5B,OALE,EAFE,EAEM,EAAM,GAGN,eAEH,CACT,CACF,CAKA,kBAAO,GACL,OAAM,aAAa,SAAW,aAAa,GAAG,UAGvC,KAFE,mDAGX","file":"/npm/macos-battery@1.1.1/lib/macos-battery.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/macos-battery@1.1.1/lib/macos-battery.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const execSh = require('exec-sh');\nconst macosVersion = require('macos-version');\n\nclass macOsBatteryHelper {\n\n  /**\n   * Returns the a promise that resolves to battery 0 (primary) charge percent.\n   * Returns a number between 0 - 100 or -1 if the battery percentage can't be\n   * determined (a desktop with no battery or some other issue.)\n   *\n   * @return {Promise<any>}\n   */\n  static getBatteryChargePercent() {\n    return new Promise((resolve,reject) => {\n      // Verify that we can support the particular OS\n      const unsupportedOsMessage = MacOsBatteryHelperInternal.supportedOs();\n      if (unsupportedOsMessage) {\n        reject(unsupportedOsMessage);\n        return;\n      }\n      // Pull the state from the OS\n      MacOsBatteryHelperInternal.getBatteryStateFromMacOs()\n        .then(currentBatteryState => {\n            // Resolve\n            resolve(MacOsBatteryHelperInternal.extractBatteryPercentFromStateArray(currentBatteryState));\n        })\n        .catch(error => reject(error));\n    });\n  }\n\n  /**\n   * Returns a promise that resolves to the battery's charge state:\n   *  discharging, charging, finishing charge, charged, no battery, undetermined\n   *\n   * @return {Promise<any>}\n   */\n  static getBatteryChargeState() {\n    return new Promise((resolve,reject) => {\n      // Verify that we can support the particular OS\n      const unsupportedOsMessage = MacOsBatteryHelperInternal.supportedOs();\n      if (unsupportedOsMessage) {\n        reject(unsupportedOsMessage);\n        return;\n      }\n      // Pull the state from the OS\n      MacOsBatteryHelperInternal.getBatteryStateFromMacOs()\n        .then(currentBatteryState => {\n            // Resolve\n            resolve(MacOsBatteryHelperInternal.extractBatteryStateFromStateArray(currentBatteryState));\n        })\n        .catch(error => reject(error));\n    });\n  }\n\n  /**\n   * Returns a promise that resolves to an object with both charge state and percent:\n   * {\n   *   percent: 99,\n   *   state: \"charged\"\n   * }\n   *\n   * @return {Promise<any>}\n   */\n  static getBatteryStateObject() {\n    return new Promise((resolve,reject) => {\n      // Verify that we can support the particular OS\n      const unsupportedOsMessage = MacOsBatteryHelperInternal.supportedOs();\n      if (unsupportedOsMessage) {\n        reject(unsupportedOsMessage);\n        return;\n      }\n      // Pull the state from the OS\n      MacOsBatteryHelperInternal.getBatteryStateFromMacOs()\n        .then(currentBatteryState => {\n          // Resolve\n          resolve({\n            percent: MacOsBatteryHelperInternal.extractBatteryPercentFromStateArray(currentBatteryState),\n            state: MacOsBatteryHelperInternal.extractBatteryStateFromStateArray(currentBatteryState)\n          });\n        })\n        .catch(error => reject(error));\n    });\n  }\n\n}\n\nmodule.exports = macOsBatteryHelper;\n\nclass MacOsBatteryHelperInternal {\n\n  /**\n   * Gets the state of the battery from the OS. Returns a Promise that resolves\n   * with an array of the stdout.\n   *\n   * stdOut can be...\n   *\n   * Line 1 could be:\n   * Now drawing from 'AC Power'\n   * Now drawing from 'Battery Power'\n   *\n   * Line 2 could be: (If the device has a battery, otherwise, this line is absent)\n   * -InternalBattery-0 (id=5308515)  34%; charging; 5:58 remaining present: true\n   * -InternalBattery-0 (id=5308515)  43%; discharging; (no estimate) present: true\n   * -InternalBattery-0 (id=5308515)\t100%; charged; (no estimate) present: true\n   * -InternalBattery-0 (id=5308515)\t100%; finishing charge; (no estimate) present: true\n   *\n   * @return {Promise<any>}\n   */\n  static getBatteryStateFromMacOs() {\n    return new Promise((resolve, reject) => {\n      // Fire off the macOs command to get Battery state\n      execSh(\"pmset -g batt\", true, function(err, stdout, stderr) {\n        // Oops. Error, so reject with it.\n        if (err) {\n          reject(stderr);\n          return;\n        }\n        // No error, process the stdout\n        const stdOutLines = stdout.split('\\n');\n        // Resolve with the \"last line\" removed since that's always empty\n        resolve(stdOutLines.splice(0, stdOutLines.length - 1));\n      });\n    });\n  }\n\n  /**\n   * Given a Battery State Array (the array-form of the multi-line output from the OS command),\n   * extracts the battery percentage value.\n   *\n   * @param stateArray\n   * @return int battery percent or -1 if it can't be determined\n   */\n  static extractBatteryPercentFromStateArray(stateArray) {\n    // With battery state, parse for what we're looking for...\n    // If the returned state array has less than 2 lines, then we don't have a battery.\n    if (stateArray.length < 2) {\n      // No battery, can't return\n      return -1;\n    } else {\n      // Pull the first battery's percent from array item 1 (the first battery)\n      // The expected string looks like this\n      // \"* -InternalBattery-0 (id=5308515) 34%; charging; 5:58 remaining present: true\"\n      const bat0 = stateArray[1];\n      // A regex that looks for 1, 2 or 3 digits followed by '%;'.\n      const myregexp = /(\\d?\\d?\\d)%;/i;\n      let percent;\n      // Apply the regex\n      const match = myregexp.exec(bat0);\n      // Did we have a match?\n      if (match) {\n        // Yes, let's return as a number\n        percent = parseInt(match[1]);\n      } else {\n        // No match, so we return -1 to indicate we can't pull a percentage\n        percent = -1;\n      }\n      return percent\n    }\n  }\n\n  /**\n   * Given a Battery State Array (the array-form of the multi-line output from the OS command),\n   * extracts the battery percentage value.\n   *\n   * @param stateArray\n   * @return string battery charge state: [charged|charging|discharging|finishing charge|no battery|undetermined]\n   */\n  static extractBatteryStateFromStateArray(stateArray) {\n    // With battery state, parse for what we're looking for...\n    // If the returned state array has less than 2 lines, then we don't have a battery.\n    if (stateArray.length < 2) {\n      // No battery, can't return\n      return 'no battery';\n    } else {\n      // Pull the first battery's charge state from array item 1 (the first battery)\n      // The expected string looks like this\n      // \"* -InternalBattery-0 (id=5308515) 34%; charging; 5:58 remaining present: true\"\n      const bat0 = stateArray[1];\n      // A regex that looks for the charging state words.\n      const myregexp = /((?:dis)?charging|charged|finishing charge);/i;\n      let state;\n      // Apply the regex\n      const match = myregexp.exec(bat0);\n      // Did we have a match?\n      if (match) {\n        // Yes, let's return the charging state\n        state = match[1];\n      } else {\n        // No match, so we return -1 to indicate we can't pull a percentage\n        state = 'undetermined';\n      }\n      return state;\n    }\n  }\n\n  /**\n   * Uses macOS version to decide if we can support the particular OS.\n   */\n  static supportedOs() {\n    if (!(macosVersion.isMacOS && macosVersion.is('>10.11'))) {\n      return 'Unsupported OS. Must run on macOS 10.11 or newer.';\n    }\n    return null;\n  }\n\n}\n"]}