Skip to content

Commit

Permalink
add codes to github
Browse files Browse the repository at this point in the history
  • Loading branch information
O-Q committed Apr 9, 2021
1 parent 3d3e4a8 commit d2cbd51
Show file tree
Hide file tree
Showing 16 changed files with 490 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
npm-debug.log
yarn-error.log
package-lock.json
.DS_Store
/lib
/coverage
.idea/
.vscode/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Fancy Scheduler

This scheduler is based on [node-schedule](https://www.npmjs.com/package/node-schedule) but fancier.

non-cron format improved with better typing.

## Installation

`npm i fancy-scheduler`

contact: `[email protected]`
6 changes: 6 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Schedule } from "./schedule";
import core from 'node-schedule';
export { core };
export { Schedule, cancelJob } from './schedule';
export { IRange, ISchedule, JobCallback, Recurrence, RecurrenceSegment, Timezone, RecurrenceSpecDateRange, Job } from './types';
export default Schedule;
15 changes: 15 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Job = exports.cancelJob = exports.Schedule = exports.core = void 0;
const schedule_1 = require("./schedule");
const node_schedule_1 = __importDefault(require("node-schedule"));
exports.core = node_schedule_1.default;
var schedule_2 = require("./schedule");
Object.defineProperty(exports, "Schedule", { enumerable: true, get: function () { return schedule_2.Schedule; } });
Object.defineProperty(exports, "cancelJob", { enumerable: true, get: function () { return schedule_2.cancelJob; } });
var types_1 = require("./types");
Object.defineProperty(exports, "Job", { enumerable: true, get: function () { return types_1.Job; } });
exports.default = schedule_1.Schedule;
47 changes: 47 additions & 0 deletions dist/schedule.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import schedule, { cancelJob, Job, JobCallback } from 'node-schedule';
import { IRange, ISchedule, ScheduleOptions, ScheduleId } from "./types";
export { cancelJob };
export declare class Schedule {
/**
* default timezone
*/
static DEFAULT_TZ: string;
private static DEFAULT_OPTIONS;
/**
* change the default value of properties in fancy `options`
*/
static config(options: ISchedule): void;
/**
* Convert fancy rule to node-schedule rule
*/
static Rule(options: ISchedule): schedule.RecurrenceRule;
/**
* Convert fancy range to node-schedule range
* @param keyName used for set the default end/start range. (optional)
*/
static Range(options: Partial<IRange>, keyName?: string): schedule.Range;
/**
* Create a fancy job
* @param options timing schedule for this job
* @param callback Job's callback
*/
static Job(options: ScheduleOptions, callback: JobCallback): Job;
/**
* Create a fancy job
* @param name optional name for this Job
* @param options timing options for this job
* @param callback Job's callback
*/
static Job(name: ScheduleId, options: ScheduleOptions, callback: JobCallback): Job;
/**
* Changes the timing of a Job, canceling all pending invocations.
* @param options The new timing options for this Job.
* @return if the job could be rescheduled, `null` otherwise.
*/
static Reschedule(job: Job | string, options: ScheduleOptions): schedule.Job;
/**
* Cancel the job
* @returns Whether the job has been cancelled with success.
*/
static CancelJob: (job: Job | string) => boolean;
}
133 changes: 133 additions & 0 deletions dist/schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Schedule = exports.cancelJob = void 0;
const node_schedule_1 = __importStar(require("node-schedule"));
Object.defineProperty(exports, "cancelJob", { enumerable: true, get: function () { return node_schedule_1.cancelJob; } });
const _extract_args_1 = require("./utils/_extract-args");
class Schedule {
/**
* change the default value of properties in fancy `options`
*/
static config(options) {
if (options.tz)
this.DEFAULT_TZ = options.tz;
Schedule.DEFAULT_OPTIONS = Object.assign(Object.assign({}, Schedule.DEFAULT_OPTIONS), options);
}
/**
* Convert fancy rule to node-schedule rule
*/
static Rule(options) {
const _a = Object.assign(Object.assign({}, Schedule.DEFAULT_OPTIONS), options), { tz } = _a, rest = __rest(_a, ["tz"]);
const rule = new node_schedule_1.RecurrenceRule();
for (const k of Object.keys(rest)) {
const obj = rest[k];
if (!obj || typeof obj !== 'object') {
rule[k] = obj;
continue;
}
if (obj instanceof Array) {
rule[k] = [];
for (const recurrence of obj.values()) {
rule[k].push(typeof recurrence === 'object' ? Schedule.Range(recurrence, k) : recurrence);
}
}
else {
rule[k] = Schedule.Range(rest[k], k);
}
}
rule.tz = tz;
return rule;
}
/**
* Convert fancy range to node-schedule range
* @param keyName used for set the default end/start range. (optional)
*/
static Range(options, keyName) {
let _end = null;
let _start = null;
if (keyName && !options.end) {
if (keyName === 'hour')
_end = 23;
else if (keyName === 'dayOfWeek')
_end = 6;
else if (keyName === 'month')
_end = 12;
else if (keyName === 'year')
_end = 2199;
else if (keyName === 'date') {
_end = 31;
_start = 1;
}
else {
_end = 59;
}
}
const { start, end, step } = Object.assign({ start: _start, end: _end, step: null }, options);
return new node_schedule_1.default.Range(start, end, step);
}
static Job(...args) {
const { name, options, callback } = _extract_args_1.extractJobArgs(args);
const job = new node_schedule_1.Job(name, callback);
let rule;
if (typeof options === 'string')
rule = options;
else
rule = Schedule.Rule(options);
const success = job.reschedule(rule);
if (!success)
console.warn('[Warn] "Options" was not valid.');
return job;
}
/**
* Changes the timing of a Job, canceling all pending invocations.
* @param options The new timing options for this Job.
* @return if the job could be rescheduled, `null` otherwise.
*/
static Reschedule(job, options) {
const spec = typeof options === 'string' ? options : Schedule.Rule(options);
return node_schedule_1.rescheduleJob(job, spec);
}
}
exports.Schedule = Schedule;
/**
* default timezone
*/
Schedule.DEFAULT_TZ = null;
Schedule.DEFAULT_OPTIONS = {
tz: Schedule.DEFAULT_TZ, hour: null, year: null, date: null, dayOfWeek: null, minute: null, month: null, second: 0
};
/**
* Cancel the job
* @returns Whether the job has been cancelled with success.
*/
Schedule.CancelJob = (job) => node_schedule_1.cancelJob(job);
21 changes: 21 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Job, Timezone, RecurrenceSpecDateRange, JobCallback } from 'node-schedule';
export { Job, Timezone, RecurrenceSpecDateRange, JobCallback };
export interface IRange {
start: number;
end: number;
step: number;
}
export interface ISchedule {
year?: RecurrenceSegment;
month?: RecurrenceSegment;
date?: RecurrenceSegment;
dayOfWeek?: RecurrenceSegment;
hour?: RecurrenceSegment;
minute?: RecurrenceSegment;
second?: RecurrenceSegment;
tz?: Timezone;
}
export declare type ScheduleId = string;
export declare type ScheduleOptions = string | ISchedule | RecurrenceSpecDateRange;
export declare type Recurrence = number | string | Partial<IRange>;
export declare type RecurrenceSegment = Recurrence | Recurrence[];
5 changes: 5 additions & 0 deletions dist/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Job = void 0;
const node_schedule_1 = require("node-schedule");
Object.defineProperty(exports, "Job", { enumerable: true, get: function () { return node_schedule_1.Job; } });
6 changes: 6 additions & 0 deletions dist/utils/_extract-args.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { JobCallback, ScheduleOptions, ScheduleId } from "../types";
export declare const extractJobArgs: (args: (ScheduleId | ScheduleOptions | JobCallback)[]) => {
options: ScheduleOptions;
name: string;
callback: JobCallback;
};
18 changes: 18 additions & 0 deletions dist/utils/_extract-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractJobArgs = void 0;
const extractJobArgs = (args) => {
let options, name, callback;
const [arg1, arg2, arg3] = args;
if (typeof arg3 == 'undefined') {
options = arg1;
callback = arg2;
}
else {
name = arg1;
options = arg2;
callback = arg3;
}
return { options, name, callback };
};
exports.extractJobArgs = extractJobArgs;
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "fancy-scheduler",
"version": "0.1.5",
"description": "Fancy Scheduler based on node-schedule",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"keywords": [
"node-schedule",
"scheduler",
"typescript"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Mahdi Shiri <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/O-Q/fancy-scheduler.git"
},
"devDependencies": {
"@types/node-schedule": "^1.3.1",
"typescript": "^4.2.3"
},
"dependencies": {
"node-schedule": "^2.0.0"
}
}
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Schedule } from "./schedule";
import core from 'node-schedule';

export { core };
export { Schedule, cancelJob } from './schedule';
export { IRange, ISchedule, JobCallback, Recurrence, RecurrenceSegment, Timezone, RecurrenceSpecDateRange, Job } from './types';

export default Schedule;
Loading

0 comments on commit d2cbd51

Please sign in to comment.