-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Durations gain validity #3611
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5076d1f
Create _isValid property for durations
marwahaha f60a3f4
Add accessor function to isValid
marwahaha 81aa62d
Return invalid date for humanize if duration is invalid
marwahaha 35ee5a5
Replace arrow function
marwahaha 0e71c08
Write duration-invalid tests
marwahaha d6afd97
Fix duration functions to follow tests
marwahaha b3604b7
Add second invalid duration test case
marwahaha f95841e
Add validity check for decimal values on non-smallest duration unit
marwahaha 058e741
Make easy changes as per code review
marwahaha a78014d
Make rest of code review changes
marwahaha 4020a7d
Create moment.duration.invalid()
marwahaha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import toInt from '../utils/to-int'; | ||
import {Duration} from './constructor'; | ||
import {createDuration} from './create'; | ||
|
||
var ordering = ['year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond']; | ||
|
||
export default function isDurationValid(m) { | ||
for (var key in m) { | ||
if (!(ordering.indexOf(key) !== -1 && (m[key] == null || !isNaN(m[key])))) { | ||
return false; | ||
} | ||
} | ||
|
||
var unitHasDecimal = false; | ||
for (var i = 0; i < ordering.length; ++i) { | ||
if (m[ordering[i]]) { | ||
if (unitHasDecimal) { | ||
return false; // only allow non-integers for smallest unit | ||
} | ||
if (parseFloat(m[ordering[i]]) !== toInt(m[ordering[i]])) { | ||
unitHasDecimal = true; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export function isValid() { | ||
return this._isValid; | ||
} | ||
|
||
export function createInvalid() { | ||
return createDuration(NaN); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { module, test } from '../qunit'; | ||
import moment from '../../moment'; | ||
|
||
module('invalid'); | ||
|
||
test('invalid duration', function (assert) { | ||
var m = moment.duration.invalid(); // should be invalid | ||
assert.equal(m.isValid(), false); | ||
assert.ok(isNaN(m.valueOf())); | ||
}); | ||
|
||
test('valid duration', function (assert) { | ||
var m = moment.duration({d: null}); // should be valid, for now | ||
assert.equal(m.isValid(), true); | ||
assert.equal(m.valueOf(), 0); | ||
}); | ||
|
||
test('invalid duration - only smallest unit can have decimal', function (assert) { | ||
var m = moment.duration({'days': 3.5, 'hours': 1.1}); // should be invalid | ||
assert.equal(m.isValid(), false); | ||
assert.ok(isNaN(m.valueOf())); // .valueOf() returns NaN for invalid durations | ||
}); | ||
|
||
test('valid duration - smallest unit can have decimal', function (assert) { | ||
var m = moment.duration({'days': 3, 'hours': 1.1}); // should be valid | ||
assert.equal(m.isValid(), true); | ||
assert.equal(m.asHours(), 73.1); | ||
}); | ||
|
||
test('invalid duration with two arguments', function (assert) { | ||
var m = moment.duration(NaN, 'days'); | ||
assert.equal(m.isValid(), false); | ||
assert.ok(isNaN(m.valueOf())); | ||
}); | ||
|
||
test('invalid duration operations', function (assert) { | ||
var invalids = [ | ||
moment.duration(NaN), | ||
moment.duration(NaN, 'days'), | ||
moment.duration.invalid() | ||
], | ||
i, | ||
invalid, | ||
valid = moment.duration(); | ||
|
||
for (i = 0; i < invalids.length; ++i) { | ||
invalid = invalids[i]; | ||
|
||
assert.ok(!invalid.add(5, 'hours').isValid(), 'invalid.add is invalid; i=' + i); | ||
assert.ok(!invalid.subtract(30, 'days').isValid(), 'invalid.subtract is invalid; i=' + i); | ||
assert.ok(!invalid.abs().isValid(), 'invalid.abs is invalid; i=' + i); | ||
assert.ok(isNaN(invalid.as('years')), 'invalid.as is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asMilliseconds()), 'invalid.asMilliseconds is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asSeconds()), 'invalid.asSeconds is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asMinutes()), 'invalid.asMinutes is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asHours()), 'invalid.asHours is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asDays()), 'invalid.asDays is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asWeeks()), 'invalid.asWeeks is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asMonths()), 'invalid.asMonths is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.asYears()), 'invalid.asYears is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.valueOf()), 'invalid.valueOf is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.get('hours')), 'invalid.get is NaN; i=' + i); | ||
|
||
assert.ok(isNaN(invalid.milliseconds()), 'invalid.milliseconds is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.seconds()), 'invalid.seconds is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.minutes()), 'invalid.minutes is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.hours()), 'invalid.hours is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.days()), 'invalid.days is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.weeks()), 'invalid.weeks is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.months()), 'invalid.months is NaN; i=' + i); | ||
assert.ok(isNaN(invalid.years()), 'invalid.years is NaN; i=' + i); | ||
|
||
assert.equal(invalid.humanize(), | ||
invalid.localeData().invalidDate(), | ||
'invalid.humanize is localized invalid duration string; i=' + i); | ||
assert.equal(invalid.toISOString(), | ||
invalid.localeData().invalidDate(), | ||
'invalid.toISOString is localized invalid duration string; i=' + i); | ||
assert.equal(invalid.toString(), | ||
invalid.localeData().invalidDate(), | ||
'invalid.toString is localized invalid duration string; i=' + i); | ||
assert.equal(invalid.toJSON(), invalid.localeData().invalidDate(), 'invalid.toJSON is null; i=' + i); | ||
assert.equal(invalid.locale(), 'en', 'invalid.locale; i=' + i); | ||
assert.equal(invalid.localeData()._abbr, 'en', 'invalid.localeData()._abbr; i=' + i); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is good. For tests it makes sense to add a helper method moment.duration.invalid(), the same way we have for moment, so you can properly construct an invalid duration.