2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-07-14 14:57:36 +00:00
|
|
|
title: $:/core/modules/utils/utils.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Various static utility functions.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-05-04 17:49:04 +00:00
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-10-21 18:30:27 +00:00
|
|
|
/*
|
|
|
|
Display a warning, in colour if we're on a terminal
|
|
|
|
*/
|
|
|
|
exports.warning = function(text) {
|
|
|
|
console.log($tw.node ? "\x1b[1;33m" + text + "\x1b[0m" : text);
|
|
|
|
}
|
|
|
|
|
2012-12-26 19:35:12 +00:00
|
|
|
/*
|
|
|
|
Trim whitespace from the start and end of a string
|
|
|
|
Thanks to Steven Levithan, http://blog.stevenlevithan.com/archives/faster-trim-javascript
|
|
|
|
*/
|
|
|
|
exports.trim = function(str) {
|
|
|
|
if(typeof str === "string") {
|
|
|
|
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
|
|
|
|
} else {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-19 12:55:04 +00:00
|
|
|
/*
|
|
|
|
Return the number of keys in an object
|
|
|
|
*/
|
|
|
|
exports.count = function(object) {
|
|
|
|
var s = 0;
|
|
|
|
$tw.utils.each(object,function() {s++;});
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
2014-04-28 14:16:31 +00:00
|
|
|
/*
|
|
|
|
Check if an array is equal by value and by reference.
|
|
|
|
*/
|
|
|
|
exports.isArrayEqual = function(array1,array2) {
|
|
|
|
if(array1 === array2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
array1 = array1 || [];
|
|
|
|
array2 = array2 || [];
|
|
|
|
if(array1.length !== array2.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return array1.every(function(value,index) {
|
|
|
|
return value === array2[index];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-05-08 14:11:29 +00:00
|
|
|
/*
|
|
|
|
Push entries onto an array, removing them first if they already exist in the array
|
2014-04-05 16:31:36 +00:00
|
|
|
array: array to modify (assumed to be free of duplicates)
|
2012-05-08 14:11:29 +00:00
|
|
|
value: a single value to push or an array of values to push
|
|
|
|
*/
|
|
|
|
exports.pushTop = function(array,value) {
|
|
|
|
var t,p;
|
|
|
|
if($tw.utils.isArray(value)) {
|
|
|
|
// Remove any array entries that are duplicated in the new values
|
2014-04-05 16:31:36 +00:00
|
|
|
if(value.length !== 0) {
|
|
|
|
if(array.length !== 0) {
|
|
|
|
if(value.length < array.length) {
|
|
|
|
for(t=0; t<value.length; t++) {
|
|
|
|
p = array.indexOf(value[t]);
|
|
|
|
if(p !== -1) {
|
|
|
|
array.splice(p,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(t=array.length-1; t>=0; t--) {
|
|
|
|
p = value.indexOf(array[t]);
|
|
|
|
if(p !== -1) {
|
|
|
|
array.splice(t,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 14:11:29 +00:00
|
|
|
}
|
2014-04-05 16:31:36 +00:00
|
|
|
// Push the values on top of the main array
|
|
|
|
array.push.apply(array,value);
|
2012-05-08 14:11:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = array.indexOf(value);
|
|
|
|
if(p !== -1) {
|
|
|
|
array.splice(p,1);
|
|
|
|
}
|
|
|
|
array.push(value);
|
|
|
|
}
|
2014-10-09 16:28:39 +00:00
|
|
|
return array;
|
2012-05-08 14:11:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Remove entries from an array
|
|
|
|
array: array to modify
|
|
|
|
value: a single value to remove, or an array of values to remove
|
|
|
|
*/
|
|
|
|
exports.removeArrayEntries = function(array,value) {
|
|
|
|
var t,p;
|
|
|
|
if($tw.utils.isArray(value)) {
|
|
|
|
for(t=0; t<value.length; t++) {
|
|
|
|
p = array.indexOf(value[t]);
|
|
|
|
if(p !== -1) {
|
|
|
|
array.splice(p,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = array.indexOf(value);
|
|
|
|
if(p !== -1) {
|
|
|
|
array.splice(p,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-13 21:31:57 +00:00
|
|
|
/*
|
|
|
|
Check whether any members of a hashmap are present in another hashmap
|
|
|
|
*/
|
|
|
|
exports.checkDependencies = function(dependencies,changes) {
|
|
|
|
var hit = false;
|
|
|
|
$tw.utils.each(changes,function(change,title) {
|
|
|
|
if($tw.utils.hop(dependencies,title)) {
|
|
|
|
hit = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return hit;
|
|
|
|
};
|
|
|
|
|
2012-12-14 13:30:10 +00:00
|
|
|
exports.extend = function(object /* [, src] */) {
|
|
|
|
$tw.utils.each(Array.prototype.slice.call(arguments, 1), function(source) {
|
|
|
|
if(source) {
|
|
|
|
for(var property in source) {
|
|
|
|
object[property] = source[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return object;
|
|
|
|
};
|
|
|
|
|
2012-05-03 20:47:16 +00:00
|
|
|
exports.deepCopy = function(object) {
|
|
|
|
var result,t;
|
|
|
|
if($tw.utils.isArray(object)) {
|
|
|
|
// Copy arrays
|
|
|
|
result = object.slice(0);
|
|
|
|
} else if(typeof object === "object") {
|
|
|
|
result = {};
|
|
|
|
for(t in object) {
|
|
|
|
if(object[t] !== undefined) {
|
|
|
|
result[t] = $tw.utils.deepCopy(object[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = object;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.extendDeepCopy = function(object,extendedProperties) {
|
|
|
|
var result = $tw.utils.deepCopy(object),t;
|
2012-05-04 17:49:04 +00:00
|
|
|
for(t in extendedProperties) {
|
2012-05-05 10:21:15 +00:00
|
|
|
if(extendedProperties[t] !== undefined) {
|
|
|
|
result[t] = $tw.utils.deepCopy(extendedProperties[t]);
|
2012-05-03 20:47:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2012-11-08 18:34:04 +00:00
|
|
|
exports.slowInSlowOut = function(t) {
|
|
|
|
return (1 - ((Math.cos(t * Math.PI) + 1) / 2));
|
|
|
|
};
|
|
|
|
|
2014-10-08 13:07:48 +00:00
|
|
|
exports.formatDateString = function(date,template) {
|
2014-10-09 09:33:08 +00:00
|
|
|
var t = template;
|
|
|
|
t = t.replace(/0hh12/g,function() {
|
|
|
|
return $tw.utils.pad($tw.utils.getHours12(date));
|
|
|
|
});
|
|
|
|
t = t.replace(/hh12/g,function() {
|
|
|
|
return $tw.utils.getHours12(date);
|
|
|
|
});
|
|
|
|
t = t.replace(/0hh/g,function() {
|
|
|
|
return $tw.utils.pad(date.getHours());
|
|
|
|
});
|
|
|
|
t = t.replace(/hh/g,function() {
|
|
|
|
return date.getHours();
|
|
|
|
});
|
|
|
|
t = t.replace(/mmm/g,function() {
|
|
|
|
return $tw.language.getString("Date/Short/Month/" + (date.getMonth() + 1));
|
|
|
|
});
|
|
|
|
t = t.replace(/0mm/g,function() {
|
|
|
|
return $tw.utils.pad(date.getMinutes());
|
|
|
|
});
|
|
|
|
t = t.replace(/mm/g,function() {
|
|
|
|
return date.getMinutes();
|
|
|
|
});
|
|
|
|
t = t.replace(/0ss/g,function() {
|
|
|
|
return $tw.utils.pad(date.getSeconds());
|
|
|
|
});
|
|
|
|
t = t.replace(/ss/g,function() {
|
|
|
|
return date.getSeconds();
|
|
|
|
});
|
|
|
|
t = t.replace(/[ap]m/g,function() {
|
|
|
|
return $tw.utils.getAmPm(date).toLowerCase();
|
|
|
|
});
|
|
|
|
t = t.replace(/[AP]M/g,function() {
|
|
|
|
return $tw.utils.getAmPm(date).toUpperCase();
|
|
|
|
});
|
|
|
|
t = t.replace(/wYYYY/g,function() {
|
|
|
|
return $tw.utils.getYearForWeekNo(date);
|
|
|
|
});
|
|
|
|
t = t.replace(/wYY/g,function() {
|
|
|
|
return $tw.utils.pad($tw.utils.getYearForWeekNo(date)-2000);
|
|
|
|
});
|
|
|
|
t = t.replace(/YYYY/g,function() {
|
|
|
|
return date.getFullYear();
|
|
|
|
});
|
|
|
|
t = t.replace(/YY/g,function() {
|
|
|
|
return $tw.utils.pad(date.getFullYear()-2000);
|
|
|
|
});
|
|
|
|
t = t.replace(/MMM/g,function() {
|
|
|
|
return $tw.language.getString("Date/Long/Month/" + (date.getMonth() + 1));
|
|
|
|
});
|
|
|
|
t = t.replace(/0MM/g,function() {
|
|
|
|
return $tw.utils.pad(date.getMonth()+1);
|
|
|
|
});
|
|
|
|
t = t.replace(/MM/g,function() {
|
|
|
|
return date.getMonth() + 1;
|
|
|
|
});
|
|
|
|
t = t.replace(/0WW/g,function() {
|
|
|
|
return $tw.utils.pad($tw.utils.getWeek(date));
|
|
|
|
});
|
|
|
|
t = t.replace(/WW/g,function() {
|
|
|
|
return $tw.utils.getWeek(date);
|
|
|
|
});
|
|
|
|
t = t.replace(/DDD/g,function() {
|
|
|
|
return $tw.language.getString("Date/Long/Day/" + date.getDay());
|
|
|
|
});
|
|
|
|
t = t.replace(/ddd/g,function() {
|
|
|
|
return $tw.language.getString("Date/Short/Day/" + date.getDay());
|
|
|
|
});
|
|
|
|
t = t.replace(/0DD/g,function() {
|
|
|
|
return $tw.utils.pad(date.getDate());
|
|
|
|
});
|
|
|
|
t = t.replace(/DDth/g,function() {
|
|
|
|
return date.getDate() + $tw.utils.getDaySuffix(date);
|
|
|
|
});
|
|
|
|
t = t.replace(/DD/g,function() {
|
|
|
|
return date.getDate();
|
|
|
|
});
|
|
|
|
t = t.replace(/TZD/g,function() {
|
|
|
|
var tz = date.getTimezoneOffset(),
|
|
|
|
atz = Math.abs(tz);
|
|
|
|
return (tz < 0 ? '+' : '-') + $tw.utils.pad(Math.floor(atz / 60)) + ':' + $tw.utils.pad(atz % 60);
|
|
|
|
});
|
2014-01-29 12:43:34 +00:00
|
|
|
t = t.replace(/\\(.)/g,"$1");
|
2012-04-30 11:23:03 +00:00
|
|
|
return t;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getAmPm = function(date) {
|
2014-10-09 09:33:08 +00:00
|
|
|
return $tw.language.getString("Date/Period/" + (date.getHours() >= 12 ? "pm" : "am"));
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getDaySuffix = function(date) {
|
2014-10-09 09:33:08 +00:00
|
|
|
return $tw.language.getString("Date/DaySuffix/" + date.getDate());
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getWeek = function(date) {
|
|
|
|
var dt = new Date(date.getTime());
|
|
|
|
var d = dt.getDay();
|
2014-10-09 09:33:08 +00:00
|
|
|
if(d === 0) {
|
|
|
|
d = 7; // JavaScript Sun=0, ISO Sun=7
|
|
|
|
}
|
|
|
|
dt.setTime(dt.getTime() + (4 - d) * 86400000);// shift day to Thurs of same week to calculate weekNo
|
|
|
|
var n = Math.floor((dt.getTime()-new Date(dt.getFullYear(),0,1) + 3600000) / 86400000);
|
|
|
|
return Math.floor(n / 7) + 1;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getYearForWeekNo = function(date) {
|
|
|
|
var dt = new Date(date.getTime());
|
|
|
|
var d = dt.getDay();
|
2014-10-09 09:33:08 +00:00
|
|
|
if(d === 0) {
|
|
|
|
d = 7; // JavaScript Sun=0, ISO Sun=7
|
|
|
|
}
|
|
|
|
dt.setTime(dt.getTime() + (4 - d) * 86400000);// shift day to Thurs of same week
|
2012-04-30 11:23:03 +00:00
|
|
|
return dt.getFullYear();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getHours12 = function(date) {
|
|
|
|
var h = date.getHours();
|
|
|
|
return h > 12 ? h-12 : ( h > 0 ? h : 12 );
|
|
|
|
};
|
|
|
|
|
2012-10-20 13:23:57 +00:00
|
|
|
/*
|
|
|
|
Convert a date delta in milliseconds into a string representation of "23 seconds ago", "27 minutes ago" etc.
|
|
|
|
delta: delta in milliseconds
|
|
|
|
Returns an object with these members:
|
|
|
|
description: string describing the delta period
|
|
|
|
updatePeriod: time in millisecond until the string will be inaccurate
|
|
|
|
*/
|
|
|
|
exports.getRelativeDate = function(delta) {
|
2014-02-05 17:44:01 +00:00
|
|
|
var futurep = false;
|
2014-02-10 17:34:10 +00:00
|
|
|
if(delta < 0) {
|
|
|
|
delta = -1 * delta;
|
2014-02-05 17:44:01 +00:00
|
|
|
futurep = true;
|
|
|
|
}
|
2012-10-20 13:23:57 +00:00
|
|
|
var units = [
|
2014-03-20 20:55:59 +00:00
|
|
|
{name: "Years", duration: 365 * 24 * 60 * 60 * 1000},
|
|
|
|
{name: "Months", duration: (365/12) * 24 * 60 * 60 * 1000},
|
|
|
|
{name: "Days", duration: 24 * 60 * 60 * 1000},
|
|
|
|
{name: "Hours", duration: 60 * 60 * 1000},
|
|
|
|
{name: "Minutes", duration: 60 * 1000},
|
|
|
|
{name: "Seconds", duration: 1000}
|
2012-10-20 13:23:57 +00:00
|
|
|
];
|
|
|
|
for(var t=0; t<units.length; t++) {
|
|
|
|
var result = Math.floor(delta / units[t].duration);
|
|
|
|
if(result >= 2) {
|
|
|
|
return {
|
2012-10-28 18:41:48 +00:00
|
|
|
delta: delta,
|
2014-03-20 20:55:59 +00:00
|
|
|
description: $tw.language.getString(
|
|
|
|
"RelativeDate/" + (futurep ? "Future" : "Past") + "/" + units[t].name,
|
|
|
|
{variables:
|
|
|
|
{period: result.toString()}
|
|
|
|
}
|
|
|
|
),
|
2012-10-20 13:23:57 +00:00
|
|
|
updatePeriod: units[t].duration
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
2012-10-28 18:41:48 +00:00
|
|
|
delta: delta,
|
2014-03-20 20:55:59 +00:00
|
|
|
description: $tw.language.getString(
|
2014-03-31 11:41:54 +00:00
|
|
|
"RelativeDate/" + (futurep ? "Future" : "Past") + "/Second",
|
2014-03-20 20:55:59 +00:00
|
|
|
{variables:
|
|
|
|
{period: "1"}
|
|
|
|
}
|
|
|
|
),
|
2012-10-20 13:23:57 +00:00
|
|
|
updatePeriod: 1000
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
// Convert & to "&", < to "<", > to ">" and " to """
|
2012-12-26 19:35:12 +00:00
|
|
|
exports.htmlEncode = function(s) {
|
2012-04-30 11:23:03 +00:00
|
|
|
if(s) {
|
|
|
|
return s.toString().replace(/&/mg,"&").replace(/</mg,"<").replace(/>/mg,">").replace(/\"/mg,""");
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Converts all HTML entities to their character equivalents
|
|
|
|
exports.entityDecode = function(s) {
|
|
|
|
var e = s.substr(1,s.length-2); // Strip the & and the ;
|
|
|
|
if(e.charAt(0) === "#") {
|
|
|
|
if(e.charAt(1) === "x" || e.charAt(1) === "X") {
|
|
|
|
return String.fromCharCode(parseInt(e.substr(2),16));
|
|
|
|
} else {
|
|
|
|
return String.fromCharCode(parseInt(e.substr(1),10));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var c = $tw.config.htmlEntities[e];
|
|
|
|
if(c) {
|
|
|
|
return String.fromCharCode(c);
|
|
|
|
} else {
|
|
|
|
return s; // Couldn't convert it as an entity, just return it raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.unescapeLineBreaks = function(s) {
|
|
|
|
return s.replace(/\\n/mg,"\n").replace(/\\b/mg," ").replace(/\\s/mg,"\\").replace(/\r/mg,"");
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns an escape sequence for given character. Uses \x for characters <=
|
|
|
|
* 0xFF to save space, \u for the rest.
|
|
|
|
*
|
|
|
|
* The code needs to be in sync with th code template in the compilation
|
|
|
|
* function for "action" nodes.
|
|
|
|
*/
|
|
|
|
// Copied from peg.js, thanks to David Majda
|
|
|
|
exports.escape = function(ch) {
|
|
|
|
var charCode = ch.charCodeAt(0);
|
2014-01-03 10:50:00 +00:00
|
|
|
if(charCode <= 0xFF) {
|
2012-04-30 11:23:03 +00:00
|
|
|
return '\\x' + $tw.utils.pad(charCode.toString(16).toUpperCase());
|
|
|
|
} else {
|
|
|
|
return '\\u' + $tw.utils.pad(charCode.toString(16).toUpperCase(),4);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Turns a string into a legal JavaScript string
|
|
|
|
// Copied from peg.js, thanks to David Majda
|
|
|
|
exports.stringify = function(s) {
|
|
|
|
/*
|
|
|
|
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
|
|
|
|
* literal except for the closing quote character, backslash, carriage return,
|
|
|
|
* line separator, paragraph separator, and line feed. Any character may
|
|
|
|
* appear in the form of an escape sequence.
|
|
|
|
*
|
|
|
|
* For portability, we also escape escape all non-ASCII characters.
|
|
|
|
*/
|
|
|
|
return s
|
|
|
|
.replace(/\\/g, '\\\\') // backslash
|
|
|
|
.replace(/"/g, '\\"') // double quote character
|
|
|
|
.replace(/'/g, "\\'") // single quote character
|
|
|
|
.replace(/\r/g, '\\r') // carriage return
|
|
|
|
.replace(/\n/g, '\\n') // line feed
|
|
|
|
.replace(/[\x80-\uFFFF]/g, exports.escape); // non-ASCII characters
|
|
|
|
};
|
|
|
|
|
2012-10-17 18:10:48 +00:00
|
|
|
/*
|
|
|
|
Escape the RegExp special characters with a preceding backslash
|
|
|
|
*/
|
|
|
|
exports.escapeRegExp = function(s) {
|
2012-11-06 17:21:56 +00:00
|
|
|
return s.replace(/[\-\/\\\^\$\*\+\?\.\(\)\|\[\]\{\}]/g, '\\$&');
|
2012-10-17 18:10:48 +00:00
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
exports.nextTick = function(fn) {
|
|
|
|
/*global window: false */
|
|
|
|
if(typeof window !== "undefined") {
|
|
|
|
// Apparently it would be faster to use postMessage - http://dbaron.org/log/20100309-faster-timeouts
|
|
|
|
window.setTimeout(fn,4);
|
|
|
|
} else {
|
|
|
|
process.nextTick(fn);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-11 09:41:13 +00:00
|
|
|
/*
|
|
|
|
Convert a hyphenated CSS property name into a camel case one
|
|
|
|
*/
|
|
|
|
exports.unHyphenateCss = function(propName) {
|
2012-10-25 13:57:48 +00:00
|
|
|
return propName.replace(/-([a-z])/gi, function(match0,match1) {
|
|
|
|
return match1.toUpperCase();
|
|
|
|
});
|
2012-06-11 09:41:13 +00:00
|
|
|
};
|
|
|
|
|
2012-10-25 21:20:27 +00:00
|
|
|
/*
|
|
|
|
Convert a camelcase CSS property name into a dashed one ("backgroundColor" --> "background-color")
|
|
|
|
*/
|
|
|
|
exports.hyphenateCss = function(propName) {
|
|
|
|
return propName.replace(/([A-Z])/g, function(match0,match1) {
|
|
|
|
return "-" + match1.toLowerCase();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-12-13 21:31:57 +00:00
|
|
|
/*
|
2013-01-15 17:50:47 +00:00
|
|
|
Parse a text reference of one of these forms:
|
|
|
|
* title
|
|
|
|
* !!field
|
|
|
|
* title!!field
|
|
|
|
* title##index
|
|
|
|
* etc
|
|
|
|
Returns an object with the following fields, all optional:
|
|
|
|
* title: tiddler title
|
|
|
|
* field: tiddler field name
|
|
|
|
* index: JSON property index
|
2012-12-13 21:31:57 +00:00
|
|
|
*/
|
|
|
|
exports.parseTextReference = function(textRef) {
|
2013-01-15 17:50:47 +00:00
|
|
|
// Separate out the title, field name and/or JSON indices
|
2014-11-21 18:16:22 +00:00
|
|
|
var reTextRef = /(?:(.*?)!!(.+))|(?:(.*?)##(.+))|(.*)/mg,
|
2014-11-21 17:07:03 +00:00
|
|
|
match = reTextRef.exec(textRef),
|
|
|
|
result = {};
|
2013-01-15 17:50:47 +00:00
|
|
|
if(match && reTextRef.lastIndex === textRef.length) {
|
|
|
|
// Return the parts
|
2014-11-21 17:07:03 +00:00
|
|
|
if(match[1]) {
|
|
|
|
result.title = match[1];
|
|
|
|
}
|
|
|
|
if(match[2]) {
|
|
|
|
result.field = match[2];
|
|
|
|
}
|
|
|
|
if(match[3]) {
|
2014-11-21 18:16:22 +00:00
|
|
|
result.title = match[3];
|
|
|
|
}
|
|
|
|
if(match[4]) {
|
|
|
|
result.index = match[4];
|
|
|
|
}
|
|
|
|
if(match[5]) {
|
|
|
|
result.title = match[5];
|
2014-11-21 17:07:03 +00:00
|
|
|
}
|
2012-12-13 21:31:57 +00:00
|
|
|
} else {
|
2014-11-21 17:07:03 +00:00
|
|
|
// If we couldn't parse it
|
|
|
|
result.title = textRef
|
2012-12-13 21:31:57 +00:00
|
|
|
}
|
2014-11-21 17:07:03 +00:00
|
|
|
return result;
|
2012-12-13 21:31:57 +00:00
|
|
|
};
|
|
|
|
|
2014-04-28 14:54:32 +00:00
|
|
|
/*
|
|
|
|
Checks whether a string is a valid fieldname
|
|
|
|
*/
|
|
|
|
exports.isValidFieldName = function(name) {
|
|
|
|
if(!name || typeof name !== "string") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
name = name.toLowerCase().trim();
|
|
|
|
var fieldValidatorRegEx = /^[a-z0-9\-\._]+$/mg;
|
|
|
|
return fieldValidatorRegEx.test(name);
|
|
|
|
};
|
|
|
|
|
2012-05-02 10:02:47 +00:00
|
|
|
/*
|
|
|
|
Extract the version number from the meta tag or from the boot file
|
|
|
|
*/
|
|
|
|
|
2012-05-19 10:29:51 +00:00
|
|
|
if($tw.browser) {
|
2012-05-02 10:02:47 +00:00
|
|
|
|
|
|
|
// Browser version
|
|
|
|
exports.extractVersionInfo = function() {
|
|
|
|
var metatags = document.getElementsByTagName("meta");
|
|
|
|
for(var t=0; t<metatags.length; t++) {
|
|
|
|
var m = metatags[t];
|
|
|
|
if(m.name === "tiddlywiki-version") {
|
2012-07-13 16:38:23 +00:00
|
|
|
return m.content;
|
2012-05-02 10:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-13 16:38:23 +00:00
|
|
|
return null;
|
2012-05-02 10:02:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Server version
|
|
|
|
exports.extractVersionInfo = function() {
|
2012-08-02 13:32:38 +00:00
|
|
|
return $tw.packageInfo.version;
|
2012-05-02 10:02:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-28 14:15:56 +00:00
|
|
|
/*
|
|
|
|
Get the animation duration in ms
|
|
|
|
*/
|
|
|
|
exports.getAnimationDuration = function() {
|
|
|
|
return parseInt($tw.wiki.getTiddlerText("$:/config/AnimationDuration","400"),10);
|
|
|
|
};
|
|
|
|
|
2013-11-28 10:53:37 +00:00
|
|
|
/*
|
|
|
|
Hash a string to a number
|
|
|
|
Derived from http://stackoverflow.com/a/15710692
|
|
|
|
*/
|
|
|
|
exports.hashString = function(str) {
|
|
|
|
return str.split("").reduce(function(a,b) {
|
|
|
|
a = ((a << 5) - a) + b.charCodeAt(0);
|
|
|
|
return a & a;
|
|
|
|
},0);
|
|
|
|
};
|
|
|
|
|
2013-11-28 14:03:08 +00:00
|
|
|
/*
|
|
|
|
Decode a base64 string
|
|
|
|
*/
|
|
|
|
exports.base64Decode = function(string64) {
|
|
|
|
if($tw.browser) {
|
|
|
|
// TODO
|
|
|
|
throw "$tw.utils.base64Decode() doesn't work in the browser";
|
|
|
|
} else {
|
|
|
|
return (new Buffer(string64,"base64")).toString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-23 12:33:00 +00:00
|
|
|
/*
|
|
|
|
Convert a hashmap into a tiddler dictionary format sequence of name:value pairs
|
|
|
|
*/
|
|
|
|
exports.makeTiddlerDictionary = function(data) {
|
|
|
|
var output = [];
|
|
|
|
for(var name in data) {
|
|
|
|
output.push(name + ": " + data[name]);
|
|
|
|
}
|
|
|
|
return output.join("\n");
|
|
|
|
};
|
|
|
|
|
2014-03-31 11:41:54 +00:00
|
|
|
/*
|
|
|
|
High resolution microsecond timer for profiling
|
|
|
|
*/
|
|
|
|
exports.timer = function(base) {
|
|
|
|
var m;
|
|
|
|
if($tw.node) {
|
|
|
|
var r = process.hrtime();
|
|
|
|
m = r[0] * 1e3 + (r[1] / 1e6);
|
2014-03-31 17:42:30 +00:00
|
|
|
} else if(window.performance) {
|
2014-03-31 11:41:54 +00:00
|
|
|
m = performance.now();
|
2014-03-31 17:42:30 +00:00
|
|
|
} else {
|
2014-04-14 08:02:52 +00:00
|
|
|
m = Date.now();
|
2014-03-31 11:41:54 +00:00
|
|
|
}
|
|
|
|
if(typeof base !== "undefined") {
|
|
|
|
m = m - base;
|
|
|
|
}
|
|
|
|
return m;
|
2014-04-28 14:16:31 +00:00
|
|
|
};
|
2014-03-31 11:41:54 +00:00
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
})();
|