index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. var has = require('has');
  3. function specifierIncluded(current, specifier) {
  4. var nodeParts = current.split('.');
  5. var parts = specifier.split(' ');
  6. var op = parts.length > 1 ? parts[0] : '=';
  7. var versionParts = (parts.length > 1 ? parts[1] : parts[0]).split('.');
  8. for (var i = 0; i < 3; ++i) {
  9. var cur = parseInt(nodeParts[i] || 0, 10);
  10. var ver = parseInt(versionParts[i] || 0, 10);
  11. if (cur === ver) {
  12. continue; // eslint-disable-line no-restricted-syntax, no-continue
  13. }
  14. if (op === '<') {
  15. return cur < ver;
  16. }
  17. if (op === '>=') {
  18. return cur >= ver;
  19. }
  20. return false;
  21. }
  22. return op === '>=';
  23. }
  24. function matchesRange(current, range) {
  25. var specifiers = range.split(/ ?&& ?/);
  26. if (specifiers.length === 0) {
  27. return false;
  28. }
  29. for (var i = 0; i < specifiers.length; ++i) {
  30. if (!specifierIncluded(current, specifiers[i])) {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. function versionIncluded(nodeVersion, specifierValue) {
  37. if (typeof specifierValue === 'boolean') {
  38. return specifierValue;
  39. }
  40. var current = typeof nodeVersion === 'undefined'
  41. ? process.versions && process.versions.node && process.versions.node
  42. : nodeVersion;
  43. if (typeof current !== 'string') {
  44. throw new TypeError(typeof nodeVersion === 'undefined' ? 'Unable to determine current node version' : 'If provided, a valid node version is required');
  45. }
  46. if (specifierValue && typeof specifierValue === 'object') {
  47. for (var i = 0; i < specifierValue.length; ++i) {
  48. if (matchesRange(current, specifierValue[i])) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. return matchesRange(current, specifierValue);
  55. }
  56. var data = require('./core.json');
  57. module.exports = function isCore(x, nodeVersion) {
  58. return has(data, x) && versionIncluded(nodeVersion, data[x]);
  59. };