Skip to content

Commit

Permalink
Quick fix to make this work with ESM in Gnome 45
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlibert committed Dec 5, 2023
1 parent bdffe00 commit 83cf0f0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 69 deletions.
67 changes: 37 additions & 30 deletions jsTodoExtensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,57 @@
*/

// eslint-disable-next-line no-unused-vars
function TodoTxtExtension(name) {
this.reset = function () {
export class TodoTxtExtension {
constructor (name) {
this.name = name;
}

reset () {
this.name = null;
this.parsingFunction = null;
};
}
// The parsing function should return an array containing
// the real value of the element, the parsed task line and
// the string representation of the value.
// eslint-disable-next-line no-unused-vars
this.parsingFunction = function (line) {
parsingFunction (line) {
return [null, null, null];
};
}
}

function HiddenExtension() {
this.name = "h";
export class HiddenExtension extends TodoTxtExtension {
constructor() {
super("h");
}

parsingFunction (line) {
var hidden = null;
var hiddenRegex = /\bh:1\b/;
var matchHidden = hiddenRegex.exec( line );
if ( matchHidden !== null ) {
hidden = true;
}
return [hidden, line.replace(hiddenRegex, ''), hidden ? '1' : null];
}
}

HiddenExtension.prototype = new TodoTxtExtension();
HiddenExtension.prototype.parsingFunction = function(line) {
var hidden = null;
var hiddenRegex = /\bh:1\b/;
var matchHidden = hiddenRegex.exec( line );
if ( matchHidden !== null ) {
hidden = true;
export class DueExtension extends TodoTxtExtension {
constructor() {
super("due");
}
return [hidden, line.replace(hiddenRegex, ''), hidden ? '1' : null];
};

function DueExtension() {
this.name = "due";
}
DueExtension.prototype = new TodoTxtExtension();
DueExtension.prototype.parsingFunction = function(line) {
var dueDate = null;
var dueRegex = /due:([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\s*/;
var matchDue = dueRegex.exec(line);
if ( matchDue !== null ) {
var datePieces = matchDue[1].split('-');
dueDate = new Date( datePieces[0], datePieces[1] - 1, datePieces[2] );
return [dueDate, line.replace(dueRegex, ''), matchDue[1]];
parsingFunction (line) {
var dueDate = null;
var dueRegex = /due:([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})\s*/;
var matchDue = dueRegex.exec(line);
if ( matchDue !== null ) {
var datePieces = matchDue[1].split('-');
dueDate = new Date( datePieces[0], datePieces[1] - 1, datePieces[2] );
return [dueDate, line.replace(dueRegex, ''), matchDue[1]];
}
return [null, null, null];
}
return [null, null, null];
};
}

// Exported functions for node
(function(exports){
Expand Down
74 changes: 35 additions & 39 deletions jsTodoTxt.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
var TodoTxtExtension;
if (typeof require === 'function') {
TodoTxtExtension = require('./jsTodoExtensions').TodoTxtExtension;
} else {
TodoTxtExtension = window.TodoTxtExtension;
}

import { TodoTxtExtension } from './jsTodoExtensions.js'
/*!
Shared members and static functions.
*/
Expand Down Expand Up @@ -95,43 +89,61 @@ var TodoTxt = {

};

function TodoTxtItem ( line, extensions ) {
export class TodoTxtItem {

constructor(line, extensions) {
// If extensions were specified, use them
if ( ("object" == typeof( extensions ) ) && (extensions.length > 0 ) ) {
this.extensions = extensions;
}
else {
this.extensions = null;
}
// If we were passed a string, parse it.
if( "string" === typeof( line ) ) {
this.parse( line );
}
else {
this.reset();
}
}



this.reset = function () {
reset () {
this.text = null;
this.priority = null;
this.complete = false;
this.completed = null;
this.date = null;
this.contexts = null;
this.projects = null;
};
}

this.dateString = function () {
dateString () {
if( this.date ) {
return this.date.getFullYear() + '-' +
( ( this.date.getMonth() + 1 < 10 ) ? '0' : '' ) + ( this.date.getMonth() + 1 ) + '-' +
( ( this.date.getDate() < 10 ) ? '0' : '' ) + this.date.getDate();
}
return null;
};
}

this.completedString = function () {
completedString () {
if( this.completed ) {
return this.completed.getFullYear() + '-' +
( ( this.completed.getMonth() + 1 < 10 ) ? '0' : '' ) + ( this.completed.getMonth() + 1 ) + '-' +
( ( this.completed.getDate() < 10 ) ? '0' : '' ) + this.completed.getDate();
}
return null;
};
}

/*!
Render this object to a string.
\returns A string representation of this object.
*/
this.toString = function () {
toString () {
var line = this.text;
if( null !== this.date ) { line = this.dateString() + ' ' + line; }
if( null !== this.priority ) { line = '(' + this.priority + ') ' + line; }
Expand All @@ -140,9 +152,9 @@ function TodoTxtItem ( line, extensions ) {
if( null !== this.projects ) { line = line + ' +' + this.projects.join( ' +' ); }
if( null !== this.contexts ) { line = line + ' @' + this.contexts.join( ' @' ); }
return line;
};
}

this.extensionStrings = function () {
extensionStrings () {
var extensionString = "";
if ( null !== this.extensions ) {
var len;
Expand All @@ -163,7 +175,7 @@ function TodoTxtItem ( line, extensions ) {
\param line A string in todo.txt format to parse.
*/
this.parse = function ( line ) {
parse ( line ) {
var date_pieces;

this.reset();
Expand Down Expand Up @@ -243,9 +255,9 @@ function TodoTxtItem ( line, extensions ) {
line = line.replace( TodoTxt._trim_re, '');

this.text = line;
};
}

this._getNumOfNulls = function( lhs, rhs ) {
_getNumOfNulls ( lhs, rhs ) {
if( lhs == null && rhs == null ) {
return 2;
}
Expand All @@ -255,7 +267,7 @@ function TodoTxtItem ( line, extensions ) {
return 0;
}

this._arraysAreEqual = function( lhs, rhs ) {
_arraysAreEqual ( lhs, rhs ) {
if( this._getNumOfNulls( lhs, rhs ) == 2 ) {
return true;
}
Expand All @@ -276,7 +288,7 @@ function TodoTxtItem ( line, extensions ) {
return true;
}

this._datesAreEqual = function( lhs, rhs ) {
_datesAreEqual ( lhs, rhs ) {
if( this._getNumOfNulls( lhs, rhs ) == 2 ) {
return true;
}
Expand All @@ -286,7 +298,7 @@ function TodoTxtItem ( line, extensions ) {
return (lhs.setHours(0,0,0,0) - rhs.setHours(0,0,0,0)) == 0;
}

this.equals = function( todo ) {
equals ( todo ) {
var dates = ["date", "completed"];
var i;
for( i in dates ) {
Expand All @@ -307,23 +319,7 @@ function TodoTxtItem ( line, extensions ) {
}
}
return true;
};

// If extensions were specified, use them
if ( ("object" == typeof( extensions ) ) && (extensions.length > 0 ) ) {
this.extensions = extensions;
}
else {
this.extensions = null;
}
// If we were passed a string, parse it.
if( "string" === typeof( line ) ) {
this.parse( line );
}
else {
this.reset();
}

}

// Exported functions for node
Expand Down

0 comments on commit 83cf0f0

Please sign in to comment.