-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #491 from OpenFn/cursor-helper
Cursor Helper
- Loading branch information
Showing
7 changed files
with
358 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@openfn/language-common': minor | ||
--- | ||
|
||
Added cursor() helper |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from './http'; | ||
export * from './references'; | ||
import parseDate from './parse-date'; | ||
|
||
export { parseDate } |
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,34 @@ | ||
import { startOfToday, startOfYesterday, subHours, subDays, startOfDay } from 'date-fns' | ||
|
||
// Helper function to parse a natural-language date string into an ISO date | ||
export default (d, startDate) => { | ||
try { | ||
if (d === 'start') { | ||
return startDate; | ||
} else if (d === 'now' || d === 'end') { | ||
return new Date() | ||
} | ||
else if (d === 'today') { | ||
return startOfToday() | ||
} | ||
else if (d === 'yesterday') { | ||
return startOfYesterday() | ||
} | ||
else if (/(hours? ago)$/.test(d)) { | ||
// return the same minute n hours ago | ||
const [diff] = d.match(/\d+/) | ||
return subHours(new Date(), diff) | ||
} | ||
else if (/(days? ago)$/.test(d)) { | ||
// return the start of today - n days | ||
const [diff] = d.match(/\d+/) | ||
return startOfDay(subDays(new Date(), diff)) | ||
} | ||
} catch(e) { | ||
console.log(`Error converting ${d} into a date`) | ||
console.log(e) | ||
} | ||
|
||
// Just return the value if we couldn't parse it | ||
return d; | ||
} |
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
Oops, something went wrong.