Skip to content
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

Convert to Typescript #228

Merged
merged 38 commits into from
Aug 3, 2018
Merged

Convert to Typescript #228

merged 38 commits into from
Aug 3, 2018

Conversation

davidgoli
Copy link
Collaborator

@davidgoli davidgoli commented Jul 31, 2018

Addresses #229

Note there is a breaking change here: the types exported from dist/es6/index.d.ts do not conform to the structure in the original index.d.ts. Using RRule as both a namespace and a class seemed overly complicated, so I simplified it, but open to discussion.

I'd say this PR probably warrants a minor version bump in the package.

package.json Outdated
@@ -16,22 +17,41 @@
"rfc"
],
"author": "Jakub Roztocil and Lars Schöning",
"main": "dist/index",
"main": "dist/es5",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💛 I believe bower.json will need to be similarly updated to point to the correct distribution

README.md Outdated
var RRuleSet = require('rrule').RRuleSet
var rrulestr = require('rrule').rrulestr
```es6
import { RRule, RRuleSet, rrulestr } from 'rrule/es6'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💛 I don't think its mentioned anywhere in the README but we should point out that you can use

import { RRule, RRuleSet, rrulestr } from 'rrule'

to pull in RRule that requires no transpiling

src/helpers.ts Outdated
@@ -15,12 +15,12 @@ const range = function (start, end) {
return rang
}

const repeat = function (value, times) {
const repeat = function<T>(value: T | T[], times: number): T[] | T[][] {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must have been "fun" to convert to typescript.

@@ -173,14 +178,14 @@ export const testRecurring = function (msg, testObj, expectedDates) {
}
}
})
}
} as TestRecurring

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💛 Let's just declare the type once

const string = rule.toString()
const rrule2 = RRule.fromString(string, rule.options.dtstart)
const str = rule.toString()
const rrule2 = RRule.fromString(str /*, rule.options.dtstart */)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💛 Should we remove this comment?

src/rrule.ts Outdated

// used by toString()
this.origOptions = {}

this.options = {}

const invalid = []
const invalid: any[] = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use unknown and some type guards instead of any here?

src/rrule.ts Outdated
}
} else if (/^[+-]?\d+$/.test(value)) {
value = Number(value)
}
key = key.toLowerCase()
// @ts-ignore

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get really intensely curious when I see @ts-ignore's, could we leave little notes about what we're avoiding with them?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't realize there were like 15 of these, maybe not a good use of time after all

@@ -720,6 +908,7 @@ export default class RRule {
// XXX: can this ever be in the array?
// - compare the actual date instead?
if (!contains(poslist, res)) poslist.push(res)
// tslint:disable-next-line:no-empty
} catch (e) {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh my... i guess this is fine for now, but, oh my...

src/rrule.ts Outdated
@@ -1167,22 +1353,23 @@ class Iterinfo {
}
}

ydayset (year, month, day) {
ydayset (_: any, __: any, ___: any) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just declare this as ydayset () { and get the same thing?

src/rrule.ts Outdated
return [range(this.yearlen), 0, this.yearlen]
}

mdayset (year, month, day) {
mdayset (_: any, month: number, __: any) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the first two args seem useful here.

src/rrulestr.ts Outdated
_handle_int (rrkwargs, name, value, options) { // eslint-disable-line
export default class RRuleStr {
// tslint:disable-next-line:variable-name
_handle_DTSTART (rrkwargs: Options, name: string, value: string, _: any) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can trim off the trailing _ args on functions in this file if we want

src/rrulestr.ts Outdated
}

_handle_FREQ (rrkwargs, name, value, options) { // eslint-disable-line
_handle_FREQ (rrkwargs: Options, _: any, value: FreqKey, __: any) {
// eslint-disable-line

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These eslint disables aren't doing anything

src/rrulestr.ts Outdated
let name: string
let value: string
let parts: string[]
let parms: any

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chicken parms
image

src/rrulestr.ts Outdated
let value: string
let parts: string[]
let parms: any
let parm: any

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chicken parm
image

src/rrulestr.ts Outdated

const invalid = []
parse (s: string, options: Partial<RRuleStrOptions> = {}): RRule | RRuleSet {
const invalid: any[] = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar thing here with any vs unknown, I'm curious how hard it would be to guard our way to inferred-stringiness

src/rrulestr.ts Outdated

RRuleStr.prototype._handle_DTSTART = function (rrkwargs, name, value, options) {
rrkwargs[name.toLowerCase()] = dateutil.untilStringToDate(value)
// tslint:disable-next-line:variable-name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do a // tslint:disable:variable-name here and // tslint:enable:variable-name after this block of stuff

const allowedMethods = ['all', 'between']
if (!contains(allowedMethods, method)) {
throw new Error('Invalid method "' + method +
'". Only all and between works with iterator.')
throw new Error(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💛 optional: template string here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I scrapped that bit altogether. Thanks, Typescript!

src/rrule.ts Outdated
52 +
pymod(
lyearlen + pymod(lyearweekday - (rr.options.wkst as number), 7),
7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting sure got interesting

src/rrule.ts Outdated
@@ -999,14 +1166,14 @@ class Iterinfo {
const l = Math.floor(32 + 2 * e + 2 * i - h - k) % 7
const m = Math.floor((a + 11 * h + 22 * l) / 451)
const month = Math.floor((h + l - 7 * m + 114) / 31)
const day = (h + l - 7 * m + 114) % 31 + 1
const day = ((h + l - 7 * m + 114) % 31) + 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Operator precedence is a little too magical sometimes, this is probably good.

@davidgoli
Copy link
Collaborator Author

@arolson101 @jakubroztocil @thefliik I believe this branch is now ready for consideration. Please take a look!

@arolson101
Copy link
Collaborator

I was holding off looking at it because of all the commits going in after you asked for review ;)
Could you fix the conflict?

@davidgoli
Copy link
Collaborator Author

@arolson101 Conflicts resolved! Unfortunately, that last merge to master didn't come with a test, so I'm trying to write one now, but that shouldn't block consideration of this PR. Thanks!

Copy link

@jorroll jorroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing pops out as me as bad, and the code is much more readable to me now. This being said, I'm still not familiar with the actual logic processing of the library, and I haven't downloaded and tested this branch so I can't speak to that.

LGTM tho 👍
and Awesome Job!

@davidgoli
Copy link
Collaborator Author

@thefliik I have plans to break down the logic later on. It's spaghetti.

@arolson101 This is ready for a look. I have 2 follow on branches after this!

@arolson101 arolson101 merged commit 490f27d into jkbrzt:master Aug 3, 2018
@arolson101
Copy link
Collaborator

I don't have time to look it over, but as long as it passes the test I'm happy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants