Commit 60f3a2ac authored by wildfirecode13's avatar wildfirecode13

1

parent efc3642b
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var EventEmitter_1 = require("./EventEmitter");
function getNum(n) {
return n >= 10 ? "" + n : "0" + n;
}
var Countdown = /** @class */ (function (_super) {
__extends(Countdown, _super);
function Countdown(text, seconds, format, delay) {
var _this = _super.call(this) || this;
console.log(_this);
_this._text = text;
_this._seconds = seconds;
_this._format = format;
_this._counter = 0;
_this._delay = delay || 1000;
return _this;
}
Countdown.prototype.getCountdown = function () {
if (this._format == 'hhmmss')
return this.getHHMMSS();
if (this._format == 'mmss')
return this.getMMSS();
};
Countdown.prototype.getHHMMSS = function () {
var left = this._seconds - this._counter;
var hours = Math.floor(left / 3600);
var minutes = Math.floor(left % 3600 / 60);
var seconds = left - hours * 3600 - minutes * 60;
return getNum(hours) + ":" + getNum(minutes) + ":" + getNum(seconds);
};
Countdown.prototype.getMMSS = function () {
var left = this._seconds - this._counter;
var minutes = Math.floor(left / 60);
var seconds = left % 60;
return getNum(minutes) + ":" + getNum(seconds);
};
Countdown.prototype.getText = function () {
return this._text.replace('{0}', this.getCountdown());
};
Countdown.prototype.start = function () {
var _this = this;
this.emit('update', this.getText());
this._timer = setInterval(function () {
_this._counter++;
_this.emit('update', _this.getText());
if (_this._counter == _this._seconds) {
_this.stop();
}
}, this._delay);
};
Countdown.prototype.stop = function () {
clearInterval(this._timer);
this.emit('complete');
};
Countdown.prototype.reset = function () {
};
return Countdown;
}(EventEmitter_1.default));
exports.default = Countdown;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventEmitter = /** @class */ (function () {
function EventEmitter() {
this.events = new Map();
}
EventEmitter.prototype.on = function (event, fn, context) {
var listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: false, fn: fn, context: context });
}
else {
this.events.set(event, [{ once: false, fn: fn, context: context }]);
}
return this;
};
EventEmitter.prototype.once = function (event, fn, context) {
var listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: true, fn: fn, context: context });
}
else {
this.events.set(event, [{ once: true, fn: fn, context: context }]);
}
return this;
};
EventEmitter.prototype.off = function (event, fn, context, once) {
var listeners = this.events.get(event);
if (listeners) {
if (fn) {
for (var i = 0; i < listeners.length; i++) {
var l = listeners[i];
if ((fn === l.fn) && (!once || l.once) && (!context || l.context === context)) {
listeners.splice(i--, 1);
}
}
}
else {
this.events.delete(event);
}
}
return this;
};
EventEmitter.prototype.offAll = function () {
this.events.clear();
return this;
};
EventEmitter.prototype.emit = function (event) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var listeners = this.events.get(event);
if (listeners) {
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
if (listener.once)
listeners.splice(i--, 1);
listener.fn.apply(listener.context, args);
}
}
return this;
};
return EventEmitter;
}());
exports.default = EventEmitter;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.add = void 0;
var add = function (a, b) {
return a + b;
};
exports.add = add;
exports.EventEmitter = exports.Countdown = void 0;
var Countdown_1 = require("./Countdown");
exports.Countdown = Countdown_1.default;
var EventEmitter_1 = require("./EventEmitter");
exports.EventEmitter = EventEmitter_1.default;
import EventEmitter from "./EventEmitter";
function getNum(n:any) {
return n >= 10 ? `${n}` : `0${n}`
}
export default class Countdown extends EventEmitter<any> {
_timer:any;
_text:any;
_seconds:any;
_format:any;
_counter:any;
_delay:any;
constructor(text:any, seconds:any, format:any, delay:any) {
super();
console.log(this)
this._text = text;
this._seconds = seconds;
this._format = format;
this._counter = 0;
this._delay = delay || 1000;
}
getCountdown() {
if (this._format == 'hhmmss')
return this.getHHMMSS();
if (this._format == 'mmss')
return this.getMMSS();
}
getHHMMSS() {
const left = this._seconds - this._counter;
const hours = Math.floor(left / 3600);
const minutes = Math.floor(left % 3600/60);
const seconds = left - hours * 3600 - minutes * 60;
return `${getNum(hours)}:${getNum(minutes)}:${getNum(seconds)}`
}
getMMSS() {
const left = this._seconds - this._counter;
const minutes = Math.floor(left / 60);
const seconds = left % 60;
return `${getNum(minutes)}:${getNum(seconds)}`
}
getText() {
return this._text.replace('{0}',this.getCountdown())
}
start() {
this.emit('update', this.getText());
this._timer = setInterval(() => {
this._counter++;
this.emit('update', this.getText());
if(this._counter==this._seconds) {
this.stop()
}
}, this._delay);
}
stop() {
clearInterval(this._timer);
this.emit('complete')
}
reset(){
}
}
\ No newline at end of file
export default class EventEmitter<T> {
private events = new Map<T, { fn: Function, context: any, once: boolean }[]>();
public on(event: T, fn: Function, context?: any): this {
let listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: false, fn, context });
}
else {
this.events.set(event, [{ once: false, fn, context }]);
}
return this;
}
public once(event: T, fn: Function, context?: any): this {
let listeners = this.events.get(event);
if (listeners) {
listeners.push({ once: true, fn, context });
}
else {
this.events.set(event, [{ once: true, fn, context }]);
}
return this;
}
public off(event: T, fn?: Function, context?: any, once?: boolean): this {
let listeners = this.events.get(event);
if (listeners) {
if (fn) {
for (let i = 0; i < listeners.length; i++) {
let l = listeners[i];
if ((fn === l.fn) && (!once || l.once) && (!context || l.context === context)) {
listeners.splice(i--, 1);
}
}
}
else {
this.events.delete(event);
}
}
return this;
}
public offAll(): this {
this.events.clear();
return this;
}
public emit(event: T, ...args: any[]): this {
let listeners = this.events.get(event);
if (listeners) {
for (let i = 0; i < listeners.length; i++) {
let listener = listeners[i];
if (listener.once) listeners.splice(i--, 1);
listener.fn.apply(listener.context, args);
}
}
return this;
}
}
\ No newline at end of file
const add = (a: number, b: number) => {
return a + b;
};
export { add };
import Countdown from "./Countdown";
import EventEmitter from "./EventEmitter";
export {
Countdown,
EventEmitter
};
......@@ -5,12 +5,15 @@
"declaration": true,
"outDir": "./dist",
"declarationDir": "./types",
"strict": true
"strict": true,
"lib": [
"DOM",
"ES2015",
]
},
"exclude": [
"node_modules",
"dist",
"types"
]
}
\ No newline at end of file
import EventEmitter from "./EventEmitter";
export default class Countdown extends EventEmitter<any> {
_timer: any;
_text: any;
_seconds: any;
_format: any;
_counter: any;
_delay: any;
constructor(text: any, seconds: any, format: any, delay: any);
getCountdown(): string | undefined;
getHHMMSS(): string;
getMMSS(): string;
getText(): any;
start(): void;
stop(): void;
reset(): void;
}
export default class EventEmitter<T> {
private events;
on(event: T, fn: Function, context?: any): this;
once(event: T, fn: Function, context?: any): this;
off(event: T, fn?: Function, context?: any, once?: boolean): this;
offAll(): this;
emit(event: T, ...args: any[]): this;
}
declare const add: (a: number, b: number) => number;
export { add };
import Countdown from "./Countdown";
import EventEmitter from "./EventEmitter";
export { Countdown, EventEmitter };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment