Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

moment-range

Package Overview
Dependencies
Maintainers
5
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moment-range - npm Package Compare versions

Comparing version 3.0.3 to 3.1.0

32

CHANGELOG.md

@@ -10,4 +10,25 @@ # Moment Range

## [3.1.0]
### Added
* Added function `isRange` as a simple and clear way to check if a variable is a DateRange
* Added moment extension `parseZoneRange` to preserve timezone of start/end dates in a new range
* Added moment extension `rangeFromInterval` to create a range between a date and a specified interval
### Changed
* Changed open-ended ranges to accept any falsy value (except 0)
* Changed `add` function: now has an option for adding adjacent ranges
* Changed *README*: tidied documentation examples
### Fixed
* Fixed typo in *README* in method `add`
* Fixed webpack output path for webpack >2.3.0
* Fixed linter errors/warnings for development
* Fixed repository URLs in *CHANGELOG*
## [3.0.3]
### Changed
* 'module' has been replaced with 'jsnext:main' to support both rollup and webpack 2

@@ -85,6 +106,7 @@

[Unreleased]: https://github.com/gf3/moment-range/compare/v3.0.3...HEAD
[3.0.2]: https://github.com/gf3/moment-range/compare/v3.0.2...v3.0.3
[3.0.2]: https://github.com/gf3/moment-range/compare/v3.0.1...v3.0.2
[3.0.1]: https://github.com/gf3/moment-range/compare/v3.0.0...v3.0.1
[3.0.0]: https://github.com/gf3/moment-range/compare/v1.0.5...v3.0.1
[Unreleased]: https://github.com/rotaready/moment-range/compare/v3.1.0...HEAD
[3.0.3]: https://github.com/rotaready/moment-range/compare/v3.0.3...v3.1.0
[3.0.2]: https://github.com/rotaready/moment-range/compare/v3.0.2...v3.0.3
[3.0.2]: https://github.com/rotaready/moment-range/compare/v3.0.1...v3.0.2
[3.0.1]: https://github.com/rotaready/moment-range/compare/v3.0.0...v3.0.1
[3.0.0]: https://github.com/rotaready/moment-range/compare/v1.0.5...v3.0.1

905

dist/moment-range.js

@@ -19,5 +19,5 @@ (function webpackUniversalModuleDefinition(root, factory) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/
/******/ }
/******/ // Create a new module (and put it into the cache)

@@ -77,3 +77,3 @@ /******/ var module = installedModules[moduleId] = {

/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 19);
/******/ return __webpack_require__(__webpack_require__.s = 2);
/******/ })

@@ -88,3 +88,3 @@ /************************************************************************/

module.exports = __webpack_require__(15)() ? Symbol : __webpack_require__(17);
module.exports = __webpack_require__(16)() ? Symbol : __webpack_require__(18);

@@ -105,7 +105,448 @@

var assign = __webpack_require__(3)
, normalizeOpts = __webpack_require__(10)
, isCallable = __webpack_require__(6)
, contains = __webpack_require__(12)
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DateRange = undefined;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.extendMoment = extendMoment;
var _moment = __webpack_require__(1);
var _moment2 = _interopRequireDefault(_moment);
var _es6Symbol = __webpack_require__(0);
var _es6Symbol2 = _interopRequireDefault(_es6Symbol);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
var INTERVALS = {
year: true,
quarter: true,
month: true,
week: true,
day: true,
hour: true,
minute: true,
second: true
};
//-----------------------------------------------------------------------------
// Date Ranges
//-----------------------------------------------------------------------------
var DateRange = exports.DateRange = function () {
function DateRange(start, end) {
_classCallCheck(this, DateRange);
var s = start;
var e = end;
if (arguments.length === 1 || end === undefined) {
if ((typeof start === 'undefined' ? 'undefined' : _typeof(start)) === 'object' && start.length === 2) {
var _start = _slicedToArray(start, 2);
s = _start[0];
e = _start[1];
} else if (typeof start === 'string') {
var _isoSplit = isoSplit(start);
var _isoSplit2 = _slicedToArray(_isoSplit, 2);
s = _isoSplit2[0];
e = _isoSplit2[1];
}
}
this.start = s || s === 0 ? (0, _moment2.default)(s) : (0, _moment2.default)(-8640000000000000);
this.end = e || e === 0 ? (0, _moment2.default)(e) : (0, _moment2.default)(8640000000000000);
}
_createClass(DateRange, [{
key: 'adjacent',
value: function adjacent(other) {
var sameStartEnd = this.start.isSame(other.end);
var sameEndStart = this.end.isSame(other.start);
return sameStartEnd && other.start.valueOf() <= this.start.valueOf() || sameEndStart && other.end.valueOf() >= this.end.valueOf();
}
}, {
key: 'add',
value: function add(other) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { adjacent: false };
if (this.overlaps(other, options)) {
return new this.constructor(_moment2.default.min(this.start, other.start), _moment2.default.max(this.end, other.end));
}
return null;
}
}, {
key: 'by',
value: function by(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
var exclusive = options.exclusive || false;
var step = options.step || 1;
var diff = Math.abs(range.start.diff(range.end, interval)) / step;
var iteration = 0;
return {
next: function next() {
var current = range.start.clone().add(iteration * step, interval);
var done = exclusive ? !(iteration < diff) : !(iteration <= diff);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'byRange',
value: function byRange(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
var step = options.step || 1;
var diff = this.valueOf() / interval.valueOf() / step;
var exclusive = options.exclusive || false;
var unit = Math.floor(diff);
var iteration = 0;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
if (unit === Infinity) {
return { done: true };
}
return {
next: function next() {
var current = (0, _moment2.default)(range.start.valueOf() + interval.valueOf() * iteration * step);
var done = unit === diff && exclusive ? !(iteration < unit) : !(iteration <= unit);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'center',
value: function center() {
var center = this.start.valueOf() + this.diff() / 2;
return (0, _moment2.default)(center);
}
}, {
key: 'clone',
value: function clone() {
return new this.constructor(this.start, this.end);
}
}, {
key: 'contains',
value: function contains(other) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false };
var start = this.start.valueOf();
var end = this.end.valueOf();
var oStart = other.valueOf();
var oEnd = other.valueOf();
if (other instanceof DateRange) {
oStart = other.start.valueOf();
oEnd = other.end.valueOf();
}
var startInRange = start < oStart || start <= oStart && !options.exclusive;
var endInRange = end > oEnd || end >= oEnd && !options.exclusive;
return startInRange && endInRange;
}
}, {
key: 'diff',
value: function diff(unit, rounded) {
return this.end.diff(this.start, unit, rounded);
}
}, {
key: 'duration',
value: function duration(unit, rounded) {
return this.diff(unit, rounded);
}
}, {
key: 'intersect',
value: function intersect(other) {
var start = this.start.valueOf();
var end = this.end.valueOf();
var oStart = other.start.valueOf();
var oEnd = other.end.valueOf();
if (start <= oStart && oStart < end && end < oEnd) {
return new this.constructor(oStart, end);
} else if (oStart < start && start < oEnd && oEnd <= end) {
return new this.constructor(start, oEnd);
} else if (oStart < start && start <= end && end < oEnd) {
return this;
} else if (start <= oStart && oStart <= oEnd && oEnd <= end) {
return other;
}
return null;
}
}, {
key: 'isEqual',
value: function isEqual(other) {
return this.start.isSame(other.start) && this.end.isSame(other.end);
}
}, {
key: 'isSame',
value: function isSame(other) {
return this.isEqual(other);
}
}, {
key: 'overlaps',
value: function overlaps(other) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { adjacent: false };
var intersect = this.intersect(other) !== null;
if (options.adjacent && !intersect) {
return this.adjacent(other);
}
return intersect;
}
}, {
key: 'reverseBy',
value: function reverseBy(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
var exclusive = options.exclusive || false;
var step = options.step || 1;
var diff = Math.abs(range.start.diff(range.end, interval)) / step;
var iteration = 0;
return {
next: function next() {
var current = range.end.clone().subtract(iteration * step, interval);
var done = exclusive ? !(iteration < diff) : !(iteration <= diff);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'reverseByRange',
value: function reverseByRange(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
var step = options.step || 1;
var diff = this.valueOf() / interval.valueOf() / step;
var exclusive = options.exclusive || false;
var unit = Math.floor(diff);
var iteration = 0;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
if (unit === Infinity) {
return { done: true };
}
return {
next: function next() {
var current = (0, _moment2.default)(range.end.valueOf() - interval.valueOf() * iteration * step);
var done = unit === diff && exclusive ? !(iteration < unit) : !(iteration <= unit);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'subtract',
value: function subtract(other) {
var start = this.start.valueOf();
var end = this.end.valueOf();
var oStart = other.start.valueOf();
var oEnd = other.end.valueOf();
if (this.intersect(other) === null) {
return [this];
} else if (oStart <= start && start < end && end <= oEnd) {
return [];
} else if (oStart <= start && start < oEnd && oEnd < end) {
return [new this.constructor(oEnd, end)];
} else if (start < oStart && oStart < end && end <= oEnd) {
return [new this.constructor(start, oStart)];
} else if (start < oStart && oStart < oEnd && oEnd < end) {
return [new this.constructor(start, oStart), new this.constructor(oEnd, end)];
} else if (start < oStart && oStart < end && oEnd < end) {
return [new this.constructor(start, oStart), new this.constructor(oStart, end)];
}
return [];
}
}, {
key: 'toDate',
value: function toDate() {
return [this.start.toDate(), this.end.toDate()];
}
}, {
key: 'toString',
value: function toString() {
return this.start.format() + '/' + this.end.format();
}
}, {
key: 'valueOf',
value: function valueOf() {
return this.end.valueOf() - this.start.valueOf();
}
}]);
return DateRange;
}();
//-----------------------------------------------------------------------------
// Moment Extensions
//-----------------------------------------------------------------------------
function extendMoment(moment) {
/**
* Build a date range.
*/
moment.range = function range(start, end) {
var m = this;
if (INTERVALS.hasOwnProperty(start)) {
return new DateRange(moment(m).startOf(start), moment(m).endOf(start));
}
return new DateRange(start, end);
};
/**
* Build a date range between a date (or now) and a specified interval.
* @param {String} interval The type of interval
* @param {Number} [count=1] The number of intervals (positive or negative)
* @param {Moment|Date} [date=moment()] The date to use
* @return {DateRange}
*/
moment.rangeFromInterval = function (interval) {
var count = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var date = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : moment();
if (!moment.isMoment(date)) date = moment(date);
if (!date.isValid()) throw new Error('Invalid date.');
var dateWithInterval = date.clone().add(count, interval);
// Handle negative interval counts by assembling the dates in chronological order.
var dates = [];
dates.push(moment.min(date, dateWithInterval));
dates.push(moment.max(date, dateWithInterval));
return new DateRange(dates);
};
/**
* Uses moment.parseZone on both the start and end of the given time interval
* to preserve the time zones on the resulting DateRange object.
* @param {string} isoTimeInterval the timeInterval to be parsed
* @return {DateRange} constructed using moments that will preserve the time zones
*/
moment.parseZoneRange = function (isoTimeInterval) {
var momentStrings = isoSplit(isoTimeInterval);
var start = moment.parseZone(momentStrings[0]);
var end = moment.parseZone(momentStrings[1]);
return new DateRange(start, end);
};
/**
* Alias of static constructor.
*/
moment.fn.range = moment.range;
/**
* Expose constructor
*/
moment.range.constructor = DateRange;
/**
* Check if the current object is a date range.
*/
moment.isRange = function (range) {
return range instanceof DateRange;
};
/**
* Check if the current moment is within a given date range.
*/
moment.fn.within = function (range) {
return range.contains(this.toDate());
};
return moment;
}
//-----------------------------------------------------------------------------
// Utility Functions
//-----------------------------------------------------------------------------
/**
* Splits an iso string into two strings.
*/
function isoSplit(isoString) {
return isoString.split('/');
}
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var assign = __webpack_require__(4)
, normalizeOpts = __webpack_require__(11)
, isCallable = __webpack_require__(7)
, contains = __webpack_require__(13)
, d;

@@ -170,3 +611,3 @@

/***/ }),
/* 3 */
/* 4 */
/***/ (function(module, exports, __webpack_require__) {

@@ -177,9 +618,9 @@

module.exports = __webpack_require__(4)()
module.exports = __webpack_require__(5)()
? Object.assign
: __webpack_require__(5);
: __webpack_require__(6);
/***/ }),
/* 4 */
/* 5 */
/***/ (function(module, exports, __webpack_require__) {

@@ -200,3 +641,3 @@

/***/ }),
/* 5 */
/* 6 */
/***/ (function(module, exports, __webpack_require__) {

@@ -207,4 +648,4 @@

var keys = __webpack_require__(7)
, value = __webpack_require__(11)
var keys = __webpack_require__(8)
, value = __webpack_require__(12)

@@ -231,3 +672,3 @@ , max = Math.max;

/***/ }),
/* 6 */
/* 7 */
/***/ (function(module, exports, __webpack_require__) {

@@ -244,3 +685,3 @@

/***/ }),
/* 7 */
/* 8 */
/***/ (function(module, exports, __webpack_require__) {

@@ -251,9 +692,9 @@

module.exports = __webpack_require__(8)()
module.exports = __webpack_require__(9)()
? Object.keys
: __webpack_require__(9);
: __webpack_require__(10);
/***/ }),
/* 8 */
/* 9 */
/***/ (function(module, exports, __webpack_require__) {

@@ -273,3 +714,3 @@

/***/ }),
/* 9 */
/* 10 */
/***/ (function(module, exports, __webpack_require__) {

@@ -288,3 +729,3 @@

/***/ }),
/* 10 */
/* 11 */
/***/ (function(module, exports, __webpack_require__) {

@@ -313,3 +754,3 @@

/***/ }),
/* 11 */
/* 12 */
/***/ (function(module, exports, __webpack_require__) {

@@ -327,3 +768,3 @@

/***/ }),
/* 12 */
/* 13 */
/***/ (function(module, exports, __webpack_require__) {

@@ -334,9 +775,9 @@

module.exports = __webpack_require__(13)()
module.exports = __webpack_require__(14)()
? String.prototype.contains
: __webpack_require__(14);
: __webpack_require__(15);
/***/ }),
/* 13 */
/* 14 */
/***/ (function(module, exports, __webpack_require__) {

@@ -356,3 +797,3 @@

/***/ }),
/* 14 */
/* 15 */
/***/ (function(module, exports, __webpack_require__) {

@@ -371,3 +812,3 @@

/***/ }),
/* 15 */
/* 16 */
/***/ (function(module, exports, __webpack_require__) {

@@ -396,3 +837,3 @@

/***/ }),
/* 16 */
/* 17 */
/***/ (function(module, exports, __webpack_require__) {

@@ -413,12 +854,12 @@

/***/ }),
/* 17 */
/* 18 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
// ES2015 Symbol polyfill for environments that do not support it (or partially support it)
// ES2015 Symbol polyfill for environments that do not (or partially) support it
var d = __webpack_require__(2)
, validateSymbol = __webpack_require__(18)
var d = __webpack_require__(3)
, validateSymbol = __webpack_require__(19)

@@ -463,3 +904,3 @@ , create = Object.create, defineProperties = Object.defineProperties

HiddenSymbol = function Symbol(description) {
if (this instanceof HiddenSymbol) throw new TypeError('TypeError: Symbol is not a constructor');
if (this instanceof HiddenSymbol) throw new TypeError('Symbol is not a constructor');
return SymbolPolyfill(description);

@@ -472,3 +913,3 @@ };

var symbol;
if (this instanceof Symbol) throw new TypeError('TypeError: Symbol is not a constructor');
if (this instanceof Symbol) throw new TypeError('Symbol is not a constructor');
if (isNativeSafe) return NativeSymbol(description);

@@ -493,4 +934,4 @@ symbol = create(HiddenSymbol.prototype);

// If there's native implementation of given symbol, let's fallback to it
// to ensure proper interoperability with other native functions e.g. Array.from
// To ensure proper interoperability with other native functions (e.g. Array.from)
// fallback to eventual native implementation of given symbol
hasInstance: d('', (NativeSymbol && NativeSymbol.hasInstance) || SymbolPolyfill('hasInstance')),

@@ -542,3 +983,3 @@ isConcatSpreadable: d('', (NativeSymbol && NativeSymbol.isConcatSpreadable) ||

/***/ }),
/* 18 */
/* 19 */
/***/ (function(module, exports, __webpack_require__) {

@@ -549,3 +990,3 @@

var isSymbol = __webpack_require__(16);
var isSymbol = __webpack_require__(17);

@@ -558,386 +999,2 @@ module.exports = function (value) {

/***/ }),
/* 19 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DateRange = undefined;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.extendMoment = extendMoment;
var _moment = __webpack_require__(1);
var _moment2 = _interopRequireDefault(_moment);
var _es6Symbol = __webpack_require__(0);
var _es6Symbol2 = _interopRequireDefault(_es6Symbol);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
var INTERVALS = {
year: true,
quarter: true,
month: true,
week: true,
day: true,
hour: true,
minute: true,
second: true
};
//-----------------------------------------------------------------------------
// Date Ranges
//-----------------------------------------------------------------------------
var DateRange = exports.DateRange = function () {
function DateRange(start, end) {
_classCallCheck(this, DateRange);
var s = start;
var e = end;
if (arguments.length === 1 || end === undefined) {
if ((typeof start === 'undefined' ? 'undefined' : _typeof(start)) === 'object' && start.length === 2) {
var _start = _slicedToArray(start, 2);
s = _start[0];
e = _start[1];
} else if (typeof start === 'string') {
var _start$split = start.split('/');
var _start$split2 = _slicedToArray(_start$split, 2);
s = _start$split2[0];
e = _start$split2[1];
}
}
this.start = s === null ? (0, _moment2.default)(-8640000000000000) : (0, _moment2.default)(s);
this.end = e === null ? (0, _moment2.default)(8640000000000000) : (0, _moment2.default)(e);
}
_createClass(DateRange, [{
key: 'adjacent',
value: function adjacent(other) {
var sameStartEnd = this.start.isSame(other.end);
var sameEndStart = this.end.isSame(other.start);
return sameStartEnd && other.start.valueOf() <= this.start.valueOf() || sameEndStart && other.end.valueOf() >= this.end.valueOf();
}
}, {
key: 'add',
value: function add(other) {
if (this.overlaps(other)) {
return new this.constructor(_moment2.default.min(this.start, other.start), _moment2.default.max(this.end, other.end));
}
return null;
}
}, {
key: 'by',
value: function by(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
var exclusive = options.exclusive || false;
var step = options.step || 1;
var diff = Math.abs(range.start.diff(range.end, interval)) / step;
var iteration = 0;
return {
next: function next() {
var current = range.start.clone().add(iteration * step, interval);
var done = exclusive ? !(iteration < diff) : !(iteration <= diff);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'byRange',
value: function byRange(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
var step = options.step || 1;
var diff = this.valueOf() / interval.valueOf() / step;
var exclusive = options.exclusive || false;
var unit = Math.floor(diff);
var iteration = 0;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
if (unit === Infinity) {
return { done: true };
}
return {
next: function next() {
var current = (0, _moment2.default)(range.start.valueOf() + interval.valueOf() * iteration * step);
var done = unit === diff && exclusive ? !(iteration < unit) : !(iteration <= unit);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'center',
value: function center() {
var center = this.start.valueOf() + this.diff() / 2;
return (0, _moment2.default)(center);
}
}, {
key: 'clone',
value: function clone() {
return new this.constructor(this.start, this.end);
}
}, {
key: 'contains',
value: function contains(other) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false };
var start = this.start.valueOf();
var end = this.end.valueOf();
var oStart = other.valueOf();
var oEnd = other.valueOf();
if (other instanceof DateRange) {
oStart = other.start.valueOf();
oEnd = other.end.valueOf();
}
var startInRange = start < oStart || start <= oStart && !options.exclusive;
var endInRange = end > oEnd || end >= oEnd && !options.exclusive;
return startInRange && endInRange;
}
}, {
key: 'diff',
value: function diff(unit, rounded) {
return this.end.diff(this.start, unit, rounded);
}
}, {
key: 'duration',
value: function duration(unit, rounded) {
return this.diff(unit, rounded);
}
}, {
key: 'intersect',
value: function intersect(other) {
var start = this.start.valueOf();
var end = this.end.valueOf();
var oStart = other.start.valueOf();
var oEnd = other.end.valueOf();
if (start <= oStart && oStart < end && end < oEnd) {
return new this.constructor(oStart, end);
} else if (oStart < start && start < oEnd && oEnd <= end) {
return new this.constructor(start, oEnd);
} else if (oStart < start && start <= end && end < oEnd) {
return this;
} else if (start <= oStart && oStart <= oEnd && oEnd <= end) {
return other;
}
return null;
}
}, {
key: 'isEqual',
value: function isEqual(other) {
return this.start.isSame(other.start) && this.end.isSame(other.end);
}
}, {
key: 'isSame',
value: function isSame(other) {
return this.isEqual(other);
}
}, {
key: 'overlaps',
value: function overlaps(other) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { adjacent: false };
var intersect = this.intersect(other) !== null;
if (options.adjacent && !intersect) {
return this.adjacent(other);
}
return intersect;
}
}, {
key: 'reverseBy',
value: function reverseBy(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
var exclusive = options.exclusive || false;
var step = options.step || 1;
var diff = Math.abs(range.start.diff(range.end, interval)) / step;
var iteration = 0;
return {
next: function next() {
var current = range.end.clone().subtract(iteration * step, interval);
var done = exclusive ? !(iteration < diff) : !(iteration <= diff);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'reverseByRange',
value: function reverseByRange(interval) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { exclusive: false, step: 1 };
var range = this;
var step = options.step || 1;
var diff = this.valueOf() / interval.valueOf() / step;
var exclusive = options.exclusive || false;
var unit = Math.floor(diff);
var iteration = 0;
return _defineProperty({}, _es6Symbol2.default.iterator, function () {
if (unit === Infinity) {
return { done: true };
}
return {
next: function next() {
var current = (0, _moment2.default)(range.end.valueOf() - interval.valueOf() * iteration * step);
var done = unit === diff && exclusive ? !(iteration < unit) : !(iteration <= unit);
iteration++;
return {
done: done,
value: done ? undefined : current
};
}
};
});
}
}, {
key: 'subtract',
value: function subtract(other) {
var start = this.start.valueOf();
var end = this.end.valueOf();
var oStart = other.start.valueOf();
var oEnd = other.end.valueOf();
if (this.intersect(other) === null) {
return [this];
} else if (oStart <= start && start < end && end <= oEnd) {
return [];
} else if (oStart <= start && start < oEnd && oEnd < end) {
return [new this.constructor(oEnd, end)];
} else if (start < oStart && oStart < end && end <= oEnd) {
return [new this.constructor(start, oStart)];
} else if (start < oStart && oStart < oEnd && oEnd < end) {
return [new this.constructor(start, oStart), new this.constructor(oEnd, end)];
} else if (start < oStart && oStart < end && oEnd < end) {
return [new this.constructor(start, oStart), new this.constructor(oStart, end)];
}
return [];
}
}, {
key: 'toDate',
value: function toDate() {
return [this.start.toDate(), this.end.toDate()];
}
}, {
key: 'toString',
value: function toString() {
return this.start.format() + '/' + this.end.format();
}
}, {
key: 'valueOf',
value: function valueOf() {
return this.end.valueOf() - this.start.valueOf();
}
}]);
return DateRange;
}();
//-----------------------------------------------------------------------------
// Moment Extensions
//-----------------------------------------------------------------------------
function extendMoment(moment) {
/**
* Build a date range.
*/
moment.range = function range(start, end) {
var m = this;
if (INTERVALS.hasOwnProperty(start)) {
return new DateRange(moment(m).startOf(start), moment(m).endOf(start));
}
return new DateRange(start, end);
};
/**
* Alias of static constructor.
*/
moment.fn.range = moment.range;
/**
* Expose constructor
*/
moment.range.constructor = DateRange;
/**
* Check if the current moment is within a given date range.
*/
moment.fn.within = function (range) {
return range.contains(this.toDate());
};
return moment;
}
/***/ })

@@ -944,0 +1001,0 @@ /******/ ]);

@@ -49,2 +49,9 @@ /**

});
describe('#isRange()', function() {
it('should determine if the current object is range', function() {
expect(moment.isRange(dr)).to.be(true);
expect(moment.isRange(m1)).to.be(false);
});
});
});

@@ -95,2 +102,20 @@

it('should allow initialization from a specified interval', function() {
const range1 = moment.rangeFromInterval('month', 5, m1); // DateRange: m1 -> m1 + 5 months
const value1 = moment.range(m1, m2);
expect(range1.toString()).equal(value1.toString());
const range2 = moment.rangeFromInterval('day', -1); // DateRange: now - 1 day -> now
const value2 = moment.range(moment().add(-1, 'd'), moment());
expect(range2.toString()).equal(value2.toString());
});
it('should allow moments to keep given time zone', function() {
const start = '2015-01-17T09:50:04+03:00';
const end = '2015-04-17T08:29:55-04:00';
const timeInterval = start + '/' + end;
const range = moment.parseZoneRange(timeInterval);
expect(range.toString()).equal(timeInterval);
});
it('should allow initialization with an array', function() {

@@ -108,3 +133,3 @@ const dr = moment.range([m1, m2]);

dr = moment.range(m1, null);
dr = moment.range(m1, false);

@@ -114,2 +139,13 @@ expect(moment.isMoment(dr.end)).to.be(true);

it('should allow initialization with Jan 1 1970', function() {
let dr = moment.range(0, 0);
expect(dr.start.year()).to.be(1970);
expect(dr.end.year()).to.be(1970);
dr = moment.range(m1, false);
expect(moment.isMoment(dr.end)).to.be(true);
});
it('should allow initialization without any arguments', function() {

@@ -220,3 +256,3 @@ const dr = moment.range();

let i = 0;
for (let n of i2) {
for (const n of i2) {
i++;

@@ -348,3 +384,3 @@ }

let i = 0;
for (let n of i2) {
for (const n of i2) {
i++;

@@ -479,3 +515,3 @@ }

let i = 0;
for (let n of i2) {
for (const n of i2) {
i++;

@@ -576,3 +612,3 @@ }

let i = 0;
for (let n of i2) {
for (const n of i2) {
i++;

@@ -893,3 +929,3 @@ }

it('should not add ranges with [---]{---} overlaps where (a=[], b={})', function() {
it('should not add ranges with [---]{---} overlaps where (a=[], b={}) by default or with adjacent: false', function() {
const dr1 = moment.range(d5, d6);

@@ -899,5 +935,13 @@ const dr2 = moment.range(d6, d7);

expect(dr1.add(dr2)).to.be(null);
expect(dr1.add(dr2, { adjacent: false })).to.be(null);
});
it('should not add ranges with {---}[---] overlaps where (a=[], b={})', function() {
it('should add ranges with [---]{---} overlaps where (a=[], b={}) with adjacent: true', function() {
const dr1 = moment.range(d5, d6);
const dr2 = moment.range(d6, d7);
expect(dr1.add(dr2, { adjacent: true }).isSame(moment.range(d5, d7))).to.be(true);
});
it('should not add ranges with {---}[---] overlaps where (a=[], b={}) by default or with adjacent: false', function() {
const dr1 = moment.range(d6, d7);

@@ -907,4 +951,12 @@ const dr2 = moment.range(d5, d6);

expect(dr1.add(dr2)).to.be(null);
expect(dr1.add(dr2, { adjacent: false })).to.be(null);
});
it('should add ranges with {---}[---] overlaps where (a=[], b={}) with adjacent: true', function() {
const dr1 = moment.range(d6, d7);
const dr2 = moment.range(d5, d6);
expect(dr1.add(dr2, { adjacent: true }).isSame(moment.range(d5, d7))).to.be(true);
});
it('should add ranges {--[===]--} overlaps where (a=[], b={})', function() {

@@ -929,2 +981,4 @@ const dr1 = moment.range(d6, d7);

expect(dr1.add(dr2).isSame(moment.range(d5, d6))).to.be(true);
expect(dr1.add(dr2, { adjacent: false }).isSame(moment.range(d5, d6))).to.be(true);
expect(dr1.add(dr2, { adjacent: true }).isSame(moment.range(d5, d6))).to.be(true);
});

@@ -931,0 +985,0 @@ });

@@ -34,8 +34,8 @@ import moment from 'moment';

else if (typeof start === 'string') {
[s, e] = start.split('/');
[s, e] = isoSplit(start);
}
}
this.start = (s === null) ? moment(-8640000000000000) : moment(s);
this.end = (e === null) ? moment(8640000000000000) : moment(e);
this.start = s || s === 0 ? moment(s) : moment(-8640000000000000);
this.end = e || e === 0 ? moment(e) : moment(8640000000000000);
}

@@ -50,4 +50,4 @@

add(other) {
if (this.overlaps(other)) {
add(other, options = { adjacent: false }) {
if (this.overlaps(other, options)) {
return new this.constructor(moment.min(this.start, other.start), moment.max(this.end, other.end));

@@ -297,2 +297,3 @@ }

}
}

@@ -320,2 +321,36 @@

/**
* Build a date range between a date (or now) and a specified interval.
* @param {String} interval The type of interval
* @param {Number} [count=1] The number of intervals (positive or negative)
* @param {Moment|Date} [date=moment()] The date to use
* @return {DateRange}
*/
moment.rangeFromInterval = function(interval, count = 1, date = moment()) {
if (!moment.isMoment(date)) date = moment(date);
if (!date.isValid()) throw new Error('Invalid date.');
const dateWithInterval = date.clone().add(count, interval);
// Handle negative interval counts by assembling the dates in chronological order.
const dates = [];
dates.push(moment.min(date, dateWithInterval));
dates.push(moment.max(date, dateWithInterval));
return new DateRange(dates);
};
/**
* Uses moment.parseZone on both the start and end of the given time interval
* to preserve the time zones on the resulting DateRange object.
* @param {string} isoTimeInterval the timeInterval to be parsed
* @return {DateRange} constructed using moments that will preserve the time zones
*/
moment.parseZoneRange = function(isoTimeInterval) {
const momentStrings = isoSplit(isoTimeInterval);
const start = moment.parseZone(momentStrings[0]);
const end = moment.parseZone(momentStrings[1]);
return new DateRange(start, end);
};
/**
* Alias of static constructor.

@@ -331,2 +366,9 @@ */

/**
* Check if the current object is a date range.
*/
moment.isRange = function(range) {
return range instanceof DateRange;
};
/**
* Check if the current moment is within a given date range.

@@ -340,1 +382,13 @@ */

}
//-----------------------------------------------------------------------------
// Utility Functions
//-----------------------------------------------------------------------------
/**
* Splits an iso string into two strings.
*/
function isoSplit(isoString) {
return isoString.split('/');
}

@@ -27,7 +27,7 @@ {

],
"homepage": "https://github.com/gf3/moment-range",
"bugs": "https://github.com/gf3/moment-range/issues",
"homepage": "https://github.com/rotaready/moment-range",
"bugs": "https://github.com/rotaready/moment-range/issues",
"main": "./dist/moment-range",
"jsnext:main": "lib/moment-range.js",
"version": "3.0.3",
"version": "3.1.0",
"engines": {

@@ -45,6 +45,6 @@ "node": "*"

"lint": "eslint ./lib/",
"prepublish": "npm run build; cp ./declarations/moment-range.js.flow ./dist",
"preversion": "npm run flow && npm run lint && npm run test",
"prepublish": "yarn run build; cp ./declarations/moment-range.js.flow ./dist",
"preversion": "yarn run flow && yarn run lint && yarn run test",
"test": "karma start ./karma.conf.js",
"version": "npm run build; cp ./declarations/moment-range.js.flow ./dist"
"version": "yarn run build; cp ./declarations/moment-range.js.flow ./dist"
},

@@ -81,8 +81,5 @@ "devDependencies": {

"type": "git",
"url": "https://git@github.com/gf3/moment-range.git"
"url": "https://git@github.com/rotaready/moment-range.git"
},
"license": {
"type": "Public Domain",
"url": "https://github.com/gf3/moment-range/raw/master/UNLICENSE"
},
"license": "Unlicense",
"dependencies": {

@@ -89,0 +86,0 @@ "es6-symbol": "^3.1.0"

@@ -1,5 +0,7 @@

# moment-range [![CircleCI](https://circleci.com/gh/gf3/moment-range.svg?style=shield)](https://circleci.com/gh/gf3/moment-range)
# moment-range [![CircleCI](https://circleci.com/gh/rotaready/moment-range.svg?style=shield)](https://circleci.com/gh/rotaready/moment-range)
Fancy date ranges for [Moment.js][moment].
> Hey there! After 5 months of inactivity, we're reviewing pull requests and issues to bring moment-range up to date. Get in touch with us [in this thread](https://github.com/rotaready/moment-range/issues/177) if you have any feedback.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->

@@ -14,2 +16,4 @@ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Create](#create)
- [rangeFromInterval](#rangefrominterval)
- [parseZoneRange](#parsezonerange)
- [Attributes](#attributes)

@@ -23,2 +27,3 @@ - [Querying](#querying)

- [Intersect](#intersect)
- [IsRange](#isrange)
- [Manipulation](#manipulation)

@@ -138,5 +143,6 @@ - [Add](#add)

const rangeUntil = moment.range(null, '2011-05-05');
const rangeFrom = moment.range('2011-03-05', null);
const rangeAllTime = moment.range(null, null);
const rangeFrom = moment.range('2011-03-05');
const rangeAllTime = moment.range();
```
Note that any falsy value except 0 is treated as a missing date, resulting in an open-ended range.

@@ -148,2 +154,31 @@ *Note:* Dates and moment objects both use a timestamp of 00:00:000 if none is

#### rangeFromInterval
You can also create a range between an interval and a specified date. This accepts positive or negative values
for `count` and the date will default to _now_ if not provided.
``` js
const interval = 'month';
const count = 4;
const date = moment('2017-07-20');
const range1 = moment.rangeFromInterval(interval, count, date); // moment.range('2017-07-20', '2017-11-20')
const range2 = moment.rangeFromInterval('month', -2, date); // moment.range('2017-05-20', '2017-07-20')
```
Note: The date can be provided as a Date, String, or Moment.
When using a negative interval, the date provided will be set as the end of the range.
#### parseZoneRange
Parses an [ISO 8601 time interval][interval] into a date range while
preserving the time zones using [moment.parseZone][parseZone].
``` js
const interval = '2015-01-17T09:50:00+03:00/2015-04-17T08:29:55-04:00';
const range = moment.parseZoneRange(interval);
range.toString(); // '2015-01-17T09:50:00+03:00/2015-04-17T08:29:55-04:00'
```
### Attributes

@@ -164,2 +199,11 @@

Many of the following examples make use of these moments:
``` js
const a = moment('2016-03-10');
const b = moment('2016-03-15');
const c = moment('2016-03-29');
const d = moment('2016-04-01');
```
#### Adjacent

@@ -170,11 +214,9 @@

``` js
const a = moment('2016-03-15');
const b = moment('2016-03-29');
const c = moment('2016-03-10');
const d = moment('2016-03-15');
const range1 = moment.range(a, b);
const range2 = moment.range(c, d);
const range2 = moment.range(b, c);
const range3 = moment.range(c, d);
range1.adjacent(range2) // true
range1.adjacent(range3) // false
```

@@ -200,11 +242,6 @@

``` js
const start = new Date(2012, 4, 1);
const end = new Date(2012, 4, 23);
const lol = new Date(2012, 4, 15);
const wat = new Date(2012, 4, 27);
const range = moment.range(start, end);
const range2 = moment.range(lol, wat);
const range = moment.range(a, c);
range.contains(lol); // true
range.contains(wat); // false
range.contains(b); // true
range.contains(d); // false
```

@@ -216,5 +253,5 @@

``` js
range.contains(end) // true
range.contains(end, { exclusive: false }) // true
range.contains(end, { exclusive: true }) // false
range.contains(c) // true
range.contains(c, { exclusive: false }) // true
range.contains(c, { exclusive: true }) // false
```

@@ -227,8 +264,5 @@

``` js
const start = new Date(2012, 4, 1);
const end = new Date(2012, 4, 23);
const when = moment('2012-05-10', 'YYYY-MM-DD');
const range = moment.range(start, end);
const range = moment.range(a, c);
when.within(range); // true
b.within(range); // true
```

@@ -241,3 +275,5 @@

``` js
range.overlaps(range2); // true
const range1 = moment.range(a, c);
const range2 = moment.range(b, d);
range1.overlaps(range2); // true
```

@@ -248,9 +284,4 @@

``` js
const a = moment('2016-03-15');
const b = moment('2016-03-20');
const c = moment('2016-03-20');
const d = moment('2016-03-25');
const range1 = moment.range(a, b);
const range2 = moment.range(c, d);
const range2 = moment.range(b, c);

@@ -264,8 +295,19 @@ range1.overlaps(range2) // false

What are the intersecting ranges?
What is the intersecting range?
``` js
range.intersect(range2); // [moment.range(lol, end)]
const range1 = moment.range(a, c);
const range2 = moment.range(b, d);
range1.intersect(range2); // moment.range(b, c)
```
#### IsRange
Is it a Range?
``` js
moment.isRange(range); // true
moment.isRange(IamNotRange); // false
```
### Manipulation

@@ -275,11 +317,25 @@

Add/combine/merge overlapping ranges.
Add/combine/merge overlapping or adjacent ranges.
``` js
range.add(range2); // [moment.range(start, wat)]
const range1 = moment.range(a, c);
const range2 = moment.range(b, d);
range1.add(range2); // moment.range(a, d)
const range3 = moment.range(new Date(2012, 3, 1), new Date(2012, 3, 15);
range.add(range3); // [null]
const range3 = moment.range(a, b);
const range4 = moment.range(c, d);
range3.add(range4); // null
```
Include adjacent ranges:
``` js
const range1 = moment.range(a, b);
const range2 = moment.range(b, c);
range1.add(range2); // null
range1.add(range2, { adjacent: false }); // null
range1.add(range2, { adjacent: true }); // moment.range(a, c)
```
#### Clone

@@ -290,10 +346,8 @@

``` js
const start = new Date(2011, 2, 5);
const end = new Date(2011, 3, 5);
const dr = moment.range(start, end);
const range1 = moment.range(a, d);
const dr2 = dr.clone();
dr2.start.add(2, 'days');
const range2 = range1.clone();
range2.start.add(2, 'days');
dr2.start.toDate() === dr.start.toDate() // false
range1.start.toDate().getTime() === range2.start.toDate().getTime() // false
```

@@ -306,3 +360,10 @@

``` js
range.subtract(range2); // [moment.range(start, lol)]
const range_ab = moment.range(a, b);
const range_bc = moment.range(b, c);
const range_cd = moment.range(c, d);
const range_ad = moment.range(a, d);
range_ad.subtract(range_bc); // [moment.range(a, b) moment.range(c, d)]
range_ac.subtract(range_bc); // [moment.range(a, b)]
range_ab.subtract(range_cd); // [moment.range(a, b)]
range_bc.subtract(range_bd); // [null]
```

@@ -312,3 +373,3 @@

Each of the iteration methods return an [Iterable][iterable], providing
Each of the iteration methods returns an [Iterable][iterable], providing
a convenient and performant interface to iterating over your ranges by a given

@@ -583,3 +644,3 @@ period.

``` sh
git clone https://git@github.com/gf3/moment-range.git
git clone https://git@github.com/rotaready/moment-range.git
```

@@ -590,3 +651,3 @@

``` sh
npm install
yarn install
```

@@ -597,4 +658,4 @@

``` sh
npm run test
npm run lint
yarn run test
yarn run lint
```

@@ -638,1 +699,2 @@

[unlicense]: http://unlicense.org/
[parseZone]: https://momentjs.com/docs/#/parsing/parse-zone/

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc