initial commit of modules with javascript library structure for Helma 1.5
This commit is contained in:
parent
72d15e11ab
commit
66876ba214
23 changed files with 6174 additions and 0 deletions
112
core/Array.js
Normal file
112
core/Array.js
Normal file
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Helma License Notice
|
||||
*
|
||||
* The contents of this file are subject to the Helma License
|
||||
* Version 2.0 (the "License"). You may not use this file except in
|
||||
* compliance with the License. A copy of the License is available at
|
||||
* http://adele.helma.org/download/helma/license.txt
|
||||
*
|
||||
* Copyright 1998-2006 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: Array.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2006/04/18 13:06:58 $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* retrieve the union set of a bunch of arrays
|
||||
* @param Array (Array2, ...) the arrays to unify
|
||||
* @return Array the union set
|
||||
*/
|
||||
Array.union = function() {
|
||||
var result = [];
|
||||
var map = {};
|
||||
for (var i=0; i<arguments.length; i+=1) {
|
||||
for (var n in arguments[i]) {
|
||||
var item = arguments[i][n];
|
||||
if (!map[item]) {
|
||||
result.push(item);
|
||||
map[item] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* retrieve the intersection set of a bunch of arrays
|
||||
* @param Array (Array2, ...) the arrays to intersect
|
||||
* @return Array the intersection set
|
||||
*/
|
||||
Array.intersection = function() {
|
||||
var all = Array.union.apply(this, arguments);
|
||||
var result = [];
|
||||
for (var n in all) {
|
||||
var chksum = 0;
|
||||
var item = all[n];
|
||||
for (var i=0; i<arguments.length; i+=1) {
|
||||
if (arguments[i].contains(item))
|
||||
chksum += 1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (chksum == arguments.length)
|
||||
result.push(item);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* return the first index position of a value
|
||||
* contained in an array
|
||||
* @param Object Array to use for checking
|
||||
* @param String|Object the String or Object to check
|
||||
*/
|
||||
Array.prototype.indexOf = function(val) {
|
||||
var i = -1;
|
||||
while (i++ < this.length -1) {
|
||||
if (this[i] == val)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* return the last index position of a value
|
||||
* contained in an array
|
||||
* @param Object Array to use for checking
|
||||
* @param String|Object the String or Object to check
|
||||
*/
|
||||
Array.prototype.lastIndexOf = function(val) {
|
||||
var i = 1;
|
||||
while (this.length - i++ >= 0) {
|
||||
if (this[i] == val)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* check if an array passed as argument contains
|
||||
* a specific value (start from end of array)
|
||||
* @param Object Array to use for checking
|
||||
* @param String|Object the String or Object to check
|
||||
*/
|
||||
Array.prototype.contains = function(val) {
|
||||
if (this.indexOf(val) > -1)
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
// prevent any newly added properties from being enumerated
|
||||
for (var i in Array)
|
||||
Array.dontEnum(i);
|
||||
for (var i in Array.prototype)
|
||||
Array.prototype.dontEnum(i);
|
185
core/Date.js
Normal file
185
core/Date.js
Normal file
|
@ -0,0 +1,185 @@
|
|||
/*
|
||||
* Helma License Notice
|
||||
*
|
||||
* The contents of this file are subject to the Helma License
|
||||
* Version 2.0 (the "License"). You may not use this file except in
|
||||
* compliance with the License. A copy of the License is available at
|
||||
* http://adele.helma.org/download/helma/license.txt
|
||||
*
|
||||
* Copyright 1998-2005 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: Date.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2006/04/18 13:06:58 $
|
||||
*/
|
||||
|
||||
|
||||
Date.ONESECOND = 1000;
|
||||
Date.ONEMINUTE = 60 * Date.ONESECOND;
|
||||
Date.ONEHOUR = 60 * Date.ONEMINUTE;
|
||||
Date.ONEDAY = 24 * Date.ONEHOUR;
|
||||
Date.ONEWEEK = 7 * Date.ONEDAY;
|
||||
Date.ONEMONTH = 30 * Date.ONEDAY;
|
||||
Date.ONEYEAR = 12 * Date.ONEMONTH;
|
||||
Date.ISOFORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||
|
||||
|
||||
/**
|
||||
* format a Date to a string
|
||||
* @param String Format pattern
|
||||
* @param Object Java Locale Object (optional)
|
||||
* @param Object Java TimeZone Object (optional)
|
||||
* @return String formatted Date
|
||||
*/
|
||||
Date.prototype.format = function (format, locale, timezone) {
|
||||
if (!format)
|
||||
return this.toString();
|
||||
var sdf = locale ? new java.text.SimpleDateFormat(format, locale)
|
||||
: new java.text.SimpleDateFormat(format);
|
||||
if (timezone && timezone != sdf.getTimeZone())
|
||||
sdf.setTimeZone(timezone);
|
||||
return sdf.format(this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* set the date/time to UTC by subtracting
|
||||
* the timezone offset
|
||||
*/
|
||||
Date.prototype.toUtc = function() {
|
||||
this.setMinutes(this.getMinutes() + this.getTimezoneOffset());
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* set the date/time to local time by adding
|
||||
* the timezone offset
|
||||
*/
|
||||
Date.prototype.toLocalTime = function() {
|
||||
this.setMinutes(this.getMinutes() - this.getTimezoneOffset());
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* returns the difference between this and another
|
||||
* date object in milliseconds
|
||||
*/
|
||||
Date.prototype.diff = function(dateObj) {
|
||||
return this.getTime() - dateObj.getTime();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* return the timespan to current date/time or a different Date object
|
||||
* @param Object parameter object containing optional properties:
|
||||
* .now = String to use if difference is < 1 minute
|
||||
* .day|days = String to use for single|multiple day(s)
|
||||
* .hour|hours = String to use for single|multiple hour(s)
|
||||
* .minute|minutes = String to use for single|multiple minute(s)
|
||||
* .date = Date object to use for calculating the timespan
|
||||
* @return Object containing properties:
|
||||
* .isFuture = (Boolean)
|
||||
* .span = (String) timespan
|
||||
* @see Date.prototype.getAge, Date.prototype.getExpiry
|
||||
*/
|
||||
Date.prototype.getTimespan = function(param) {
|
||||
if (!param)
|
||||
param = {date: new Date()};
|
||||
else if (!param.date)
|
||||
param.date = new Date();
|
||||
|
||||
var result = {isFuture: this > param.date};
|
||||
var diff = Math.abs(param.date.diff(this));
|
||||
var age = {days: Math.floor((diff % Date.ONEMONTH) / Date.ONEDAY),
|
||||
hours: Math.floor((diff % Date.ONEDAY) / Date.ONEHOUR),
|
||||
minutes: Math.floor((diff % Date.ONEHOUR) / Date.ONEMINUTE)};
|
||||
|
||||
res.push();
|
||||
if (diff < Date.ONEMINUTE)
|
||||
res.write(param.now || "now");
|
||||
else {
|
||||
var arr = [{one: "day", many: "days"},
|
||||
{one: "hour", many: "hours"},
|
||||
{one: "minute", many: "minutes"}];
|
||||
for (var i in arr) {
|
||||
var value = age[arr[i].many];
|
||||
if (value != 0) {
|
||||
var prop = (value == 1 ? arr[i].one : arr[i].many);
|
||||
res.write(value);
|
||||
res.write(" ");
|
||||
res.write(param[prop] || prop);
|
||||
if (i < arr.length -1)
|
||||
res.write(param.delimiter || ", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
result.span = res.pop();
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* return the past timespan between this Date object and
|
||||
* the current Date or a different Date object
|
||||
* @see Date.prototype.getTimespan
|
||||
*/
|
||||
Date.prototype.getAge = function(param) {
|
||||
var age = this.getTimespan(param);
|
||||
if (!age.isFuture)
|
||||
return age.span;
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* return the future timespan between this Date object and
|
||||
* the current Date or a different Date object
|
||||
* @see Date.prototype.getTimespan
|
||||
*/
|
||||
Date.prototype.getExpiry = function(param) {
|
||||
var age = this.getTimespan(param);
|
||||
if (age.isFuture)
|
||||
return age.span;
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* checks if a date object equals another date object
|
||||
* @param Object Date object to compare
|
||||
* @param Int indicating how far the comparison should go
|
||||
* @return Boolean
|
||||
*/
|
||||
Date.prototype.equals = function(date, extend) {
|
||||
if (!extend)
|
||||
var extend = Date.ONEDAY;
|
||||
switch (extend) {
|
||||
case Date.ONESECOND:
|
||||
if (this.getSeconds() != date.getSeconds())
|
||||
return false;
|
||||
case Date.ONEMINUTE:
|
||||
if (this.getMinutes() != date.getMinutes())
|
||||
return false;
|
||||
case Date.ONEHOUR:
|
||||
if (this.getHours() != date.getHours())
|
||||
return false;
|
||||
case Date.ONEDAY:
|
||||
if (this.getDate() != date.getDate())
|
||||
return false;
|
||||
case Date.ONEMONTH:
|
||||
if (this.getMonth() != date.getMonth())
|
||||
return false;
|
||||
case Date.ONEYEAR:
|
||||
if (this.getFullYear() != date.getFullYear())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
// prevent any newly added properties from being enumerated
|
||||
for (var i in Date)
|
||||
Date.dontEnum(i);
|
||||
for (var i in Date.prototype)
|
||||
Date.prototype.dontEnum(i);
|
68
core/Number.js
Normal file
68
core/Number.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Helma License Notice
|
||||
*
|
||||
* The contents of this file are subject to the Helma License
|
||||
* Version 2.0 (the "License"). You may not use this file except in
|
||||
* compliance with the License. A copy of the License is available at
|
||||
* http://adele.helma.org/download/helma/license.txt
|
||||
*
|
||||
* Copyright 1998-2006 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: Number.js,v $
|
||||
* $Author: hannes $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2006/04/18 13:06:58 $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* factory to create functions for sorting objects in an array
|
||||
* @param String name of the field each object is compared with
|
||||
* @param Number order (ascending or descending)
|
||||
* @return Function ready for use in Array.prototype.sort
|
||||
*/
|
||||
Number.Sorter = function(field, order) {
|
||||
if (!order)
|
||||
order = 1;
|
||||
return function(a, b) {
|
||||
return (a[field] - b[field]) * order;
|
||||
};
|
||||
};
|
||||
|
||||
Number.Sorter.ASC = 1;
|
||||
Number.Sorter.DESC = -1;
|
||||
|
||||
|
||||
/**
|
||||
* format a Number to a String
|
||||
* @param String Format pattern
|
||||
* @return String Number formatted to a String
|
||||
* FIXME: this might need some localisation
|
||||
*/
|
||||
Number.prototype.format = function(fmt) {
|
||||
var df = fmt ? new java.text.DecimalFormat(fmt)
|
||||
: new java.text.DecimalFormat("#,##0.00");
|
||||
return df.format(0+this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* return the percentage of a Number
|
||||
* according to a given total Number
|
||||
* @param Int Total
|
||||
* @param String Format Pattern
|
||||
* @return Int Percentage
|
||||
*/
|
||||
Number.prototype.toPercent = function(total, fmt) {
|
||||
var p = this / (total / 100);
|
||||
if (!fmt)
|
||||
return Math.round(p * 100) / 100;
|
||||
return p.format(fmt);
|
||||
};
|
||||
|
||||
|
||||
// prevent any newly added properties from being enumerated
|
||||
for (var i in Number)
|
||||
Number.dontEnum(i);
|
||||
for (var i in Number.prototype)
|
||||
Number.prototype.dontEnum(i);
|
105
core/Object.js
Normal file
105
core/Object.js
Normal file
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Helma License Notice
|
||||
*
|
||||
* The contents of this file are subject to the Helma License
|
||||
* Version 2.0 (the "License"). You may not use this file except in
|
||||
* compliance with the License. A copy of the License is available at
|
||||
* http://adele.helma.org/download/helma/license.txt
|
||||
*
|
||||
* Copyright 1998-2006 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: Object.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.6 $
|
||||
* $Date: 2006/04/18 13:06:58 $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* copy the properties of an object into
|
||||
* a new object
|
||||
* @param Object the source object
|
||||
* @param Object the (optional) target object
|
||||
* @return Object the resulting object
|
||||
*/
|
||||
Object.prototype.clone = function(clone, recursive) {
|
||||
if (!clone)
|
||||
clone = new this.constructor();
|
||||
var value;
|
||||
for (var propName in this) {
|
||||
value = this[propName];
|
||||
if (recursive && (value.constructor == HopObject || value.constructor == Object)) {
|
||||
clone[propName] = value.clone(new value.constructor(), recursive);
|
||||
} else {
|
||||
clone[propName] = value;
|
||||
}
|
||||
}
|
||||
return clone;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* reduce an extended object (ie. a HopObject)
|
||||
* to a generic javascript object
|
||||
* @param HopObject the HopObject to be reduced
|
||||
* @return Object the resulting generic object
|
||||
*/
|
||||
Object.prototype.reduce = function(recursive) {
|
||||
var result = {};
|
||||
for (var i in this) {
|
||||
if (this[i] instanceof HopObject == false)
|
||||
result[i] = this[i];
|
||||
else if (recursive)
|
||||
result[i] = this.reduce(true);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* print the contents of an object for debugging
|
||||
* @param Object the object to dump
|
||||
* @param Boolean recursive flag (if true, dump child objects, too)
|
||||
*/
|
||||
Object.prototype.dump = function(recursive) {
|
||||
var beginList = "<ul>";
|
||||
var endList = "</ul>";
|
||||
var beginItem = "<li>";
|
||||
var endItem = "</li>";
|
||||
var beginKey = "<strong>";
|
||||
var endKey = ":</strong> ";
|
||||
res.write(beginList);
|
||||
for (var p in this) {
|
||||
res.write(beginItem);
|
||||
res.write(beginKey);
|
||||
res.write(p);
|
||||
res.write(endKey);
|
||||
if (recursive && typeof this[p] == "object") {
|
||||
var recurse = true;
|
||||
var types = [Function, Date, String, Number];
|
||||
for (var i in types) {
|
||||
if (this[p] instanceof types[i]) {
|
||||
recurse = false
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (recurse == true)
|
||||
this[p].dump(true);
|
||||
else {
|
||||
res.write(this[p].toSource());
|
||||
}
|
||||
} else if (this[p]) {
|
||||
res.write(encode(this[p].toSource()));
|
||||
}
|
||||
res.write(endItem);
|
||||
}
|
||||
res.write(endList);
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
// prevent any newly added properties from being enumerated
|
||||
for (var i in Object)
|
||||
Object.dontEnum(i);
|
||||
for (var i in Object.prototype)
|
||||
Object.prototype.dontEnum(i);
|
638
core/String.js
Normal file
638
core/String.js
Normal file
|
@ -0,0 +1,638 @@
|
|||
/*
|
||||
* Helma License Notice
|
||||
*
|
||||
* The contents of this file are subject to the Helma License
|
||||
* Version 2.0 (the "License"). You may not use this file except in
|
||||
* compliance with the License. A copy of the License is available at
|
||||
* http://adele.helma.org/download/helma/license.txt
|
||||
*
|
||||
* Copyright 1998-2006 Helma Software. All Rights Reserved.
|
||||
*
|
||||
* $RCSfile: helma.String.js,v $
|
||||
* $Author: czv $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2006/04/18 13:06:58 $
|
||||
*/
|
||||
|
||||
|
||||
String.ANUMPATTERN = /[^a-zA-Z0-9]/;
|
||||
String.APATTERN = /[^a-zA-Z]/;
|
||||
String.NUMPATTERN = /[^0-9]/;
|
||||
String.FILEPATTERN = /[^a-zA-Z0-9-_\. ]/;
|
||||
String.HEXPATTERN = /[^a-fA-F0-9]/;
|
||||
String.EMAILPATTERN = /^.+@.+\.[a-zA-Z]+$/;
|
||||
String.URLPATTERN = /^([^:]*):\/\/+(?:([^\/]*):)?(?:([^\/]*)@)?([\w\-_.]*[^.])(\/[^?]*)?(?:\?(.*))?$/;
|
||||
String.LEFT = -1
|
||||
String.BALANCE = 0
|
||||
String.RIGHT = 1
|
||||
String.ISOFORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
|
||||
|
||||
|
||||
/**
|
||||
* factory to create functions for sorting objects in an array
|
||||
* @param String name of the field each object is compared with
|
||||
* @param Number order (ascending or descending)
|
||||
* @return Function ready for use in Array.prototype.sort
|
||||
*/
|
||||
String.Sorter = function(field, order) {
|
||||
if (!order)
|
||||
order = 1;
|
||||
var key = field + ":" + order;
|
||||
if (!String.Sorter.cache[key]) {
|
||||
String.Sorter.cache[key] = function(a, b) {
|
||||
var str1 = String(a[field] || "").toLowerCase();
|
||||
var str2 = String(b[field] || "").toLowerCase();
|
||||
if (str1 > str2)
|
||||
return order * 1;
|
||||
if (str1 < str2)
|
||||
return order * -1;
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
return String.Sorter.cache[key];
|
||||
};
|
||||
|
||||
String.Sorter.ASC = 1;
|
||||
String.Sorter.DESC = -1;
|
||||
String.Sorter.cache = {};
|
||||
|
||||
|
||||
/**
|
||||
* create a string from a bunch of substrings
|
||||
* @param String one or more strings as arguments
|
||||
* @return String the resulting string
|
||||
*/
|
||||
String.compose = function() {
|
||||
res.push();
|
||||
for (var i=0; i<arguments.length; i++)
|
||||
res.write(arguments[i]);
|
||||
return res.pop();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* creates a random string (numbers and chars)
|
||||
* @param len length of key
|
||||
* @param mode determines which letters to use. null or 0 = all letters;
|
||||
* 1 = skip 0, 1, l and o which can easily be mixed with numbers;
|
||||
* 2 = use numbers only
|
||||
* @returns random string
|
||||
*/
|
||||
String.random = function(len, mode) {
|
||||
if (mode == 2) {
|
||||
var x = Math.random() * Math.pow(10,len);
|
||||
return Math.floor(x);
|
||||
}
|
||||
var keystr = "";
|
||||
for (var i=0; i<len; i++) {
|
||||
var x = Math.floor((Math.random() * 36));
|
||||
if (mode == 1) {
|
||||
// skip 0,1
|
||||
x = (x<2) ? x + 2 : x;
|
||||
// don't use the letters l (charCode 21+87) and o (24+87)
|
||||
x = (x==21) ? 22 : x;
|
||||
x = (x==24) ? 25 : x;
|
||||
}
|
||||
if (x<10) {
|
||||
keystr += String(x);
|
||||
} else {
|
||||
keystr += String.fromCharCode(x+87);
|
||||
}
|
||||
}
|
||||
return keystr;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* append one string onto another and add some "glue"
|
||||
* if none of the strings is empty or null.
|
||||
* @param String the first string
|
||||
* @param String the string to be appended onto the first one
|
||||
* @param String the "glue" to be inserted between both strings
|
||||
* @return String the resulting string
|
||||
*/
|
||||
String.join = function(str1, str2, glue) {
|
||||
if (glue == null)
|
||||
glue = "";
|
||||
if (str1 && str2)
|
||||
return str1 + glue + str2;
|
||||
else if (str2)
|
||||
return str2;
|
||||
return str1;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* checks if a date format pattern is correct
|
||||
* @return Boolean true if the pattern is correct
|
||||
*/
|
||||
String.prototype.isDateFormat = function() {
|
||||
try {
|
||||
new java.text.SimpleDateFormat(this);
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* parse a timestamp into a date object. This is used when users
|
||||
* want to set createtime explicitly when creating/editing stories.
|
||||
* @param String date format to be applied
|
||||
* @param Object Java TimeZone Object (optional)
|
||||
* @return Object contains the resulting date
|
||||
*/
|
||||
String.prototype.toDate = function(format, timezone) {
|
||||
var sdf = res.data._dateformat;
|
||||
if (!sdf) {
|
||||
sdf = new java.text.SimpleDateFormat(format);
|
||||
res.data._dateformat = sdf;
|
||||
} else if (format != sdf.toPattern())
|
||||
sdf.applyPattern(format);
|
||||
if (timezone && timezone != sdf.getTimeZone())
|
||||
sdf.setTimeZone(timezone);
|
||||
try {
|
||||
return new Date(sdf.parse(this).getTime());
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function checks if the string passed contains any characters that
|
||||
* are forbidden in URLs and tries to create a java.net.URL from it
|
||||
* FIXME: probably deprecated -> helma.Url
|
||||
* @return Boolean
|
||||
* @see helma.Url.PATTERN
|
||||
*/
|
||||
String.prototype.isUrl = function() {
|
||||
if (String.URLPATTERN.test(this))
|
||||
return true;
|
||||
try {
|
||||
return new java.net.URL(this);
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function checks if the string passed contains any characters
|
||||
* that are forbidden in image- or filenames
|
||||
* @return Boolean
|
||||
*/
|
||||
String.prototype.isFileName = function() {
|
||||
return !String.FILEPATTERN.test(this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function cleans the string passed as argument from any characters
|
||||
* that are forbidden or shouldn't be used in filenames
|
||||
* @return Boolean
|
||||
*/
|
||||
String.prototype.toFileName = function() {
|
||||
return this.replace(new RegExp(String.FILEPATTERN.source, "g"), "");
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function checks a string for a valid color value in hexadecimal format.
|
||||
* it may also contain # as first character
|
||||
* @returns Boolean false, if string length (without #) > 6 or < 6 or
|
||||
* contains any character which is not a valid hex value
|
||||
*/
|
||||
String.prototype.isHexColor = function() {
|
||||
var str = this;
|
||||
if (this.indexOf("#") == 0)
|
||||
str = this.substring(1);
|
||||
if (str.length != 6)
|
||||
return false;
|
||||
return !String.HEXPATTERN.test(str);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* converts a string into a hexadecimal color
|
||||
* representation (e.g. "ffcc33"). also knows how to
|
||||
* convert a color string like "rgb (255, 204, 51)".
|
||||
* @return String the resulting hex color (w/o "#")
|
||||
*/
|
||||
String.prototype.toHexColor = function() {
|
||||
if (this.startsWith("rgb")) {
|
||||
res.push();
|
||||
var col = this.replace(/[^0-9,]/g,"");
|
||||
var parts = col.split(",");
|
||||
for (var i in parts) {
|
||||
var num = parseInt(parts[i], 10);
|
||||
var hex = num.toString(16);
|
||||
res.write(hex.pad("0", 2, String.LEFT));
|
||||
}
|
||||
return res.pop();
|
||||
}
|
||||
var col = this.replace(new RegExp(String.HEXPATTERN.source), "");
|
||||
return col.toLowerCase().pad("0", 6, String.LEFT);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function returns true if the string contains
|
||||
* only a-z and 0-9 (case insensitive!)
|
||||
* @return Boolean true in case string is alpha, false otherwise
|
||||
*/
|
||||
String.prototype.isAlphanumeric = function() {
|
||||
if (!this.length)
|
||||
return false;
|
||||
return !String.ANUMPATTERN.test(this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function cleans a string by throwing away all
|
||||
* non-alphanumeric characters
|
||||
* @return cleaned string
|
||||
*/
|
||||
String.prototype.toAlphanumeric = function() {
|
||||
return this.replace(new RegExp(String.ANUMPATTERN.source, "g"), "");
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function returns true if the string contains
|
||||
* only characters a-z
|
||||
* @return Boolean true in case string is alpha, false otherwise
|
||||
*/
|
||||
String.prototype.isAlpha = function() {
|
||||
if (!this.length)
|
||||
return false;
|
||||
return !String.APATTERN.test(this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function returns true if the string contains
|
||||
* only 0-9
|
||||
* @return Boolean true in case string is numeric, false otherwise
|
||||
*/
|
||||
String.prototype.isNumeric = function() {
|
||||
if (!this.length)
|
||||
return false;
|
||||
return !String.NUMPATTERN.test(this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* transforms the first n characters of a string to uppercase
|
||||
* @param Number amount of characters to transform
|
||||
* @return String the resulting string
|
||||
*/
|
||||
String.prototype.capitalize = function(limit) {
|
||||
if (limit == null)
|
||||
limit = 1;
|
||||
var head = this.substring(0, limit);
|
||||
var tail = this.substring(limit, this.length);
|
||||
return head.toUpperCase() + tail.toLowerCase();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* transforms the first n characters of each
|
||||
* word in a string to uppercase
|
||||
* @return String the resulting string
|
||||
*/
|
||||
String.prototype.titleize = function() {
|
||||
var parts = this.split(" ");
|
||||
res.push();
|
||||
for (var i in parts) {
|
||||
res.write(parts[i].capitalize());
|
||||
if (i < parts.length-1)
|
||||
res.write(" ");
|
||||
}
|
||||
return res.pop();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* translates all characters of a string into HTML entities
|
||||
* @return String translated result
|
||||
*/
|
||||
String.prototype.entitize = function() {
|
||||
res.push();
|
||||
for (var i=0; i<this.length; i++) {
|
||||
res.write("&#");
|
||||
res.write(this.charCodeAt(i).toString());
|
||||
res.write(";");
|
||||
}
|
||||
return res.pop();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* breaks up a string into two parts called
|
||||
* head and tail at the given position
|
||||
* don't apply this to HTML, i.e. use stripTags() in advance
|
||||
* @param Number number of charactrers or of segments separated by the delimiter
|
||||
* @param String pre-/suffix to be pre-/appended to shortened string
|
||||
* @param String delimiter
|
||||
* @return Object containing head and tail properties
|
||||
*/
|
||||
String.prototype.embody = function(limit, clipping, delimiter) {
|
||||
if (typeof(limit) == "string")
|
||||
limit = parseInt(limit, 10);
|
||||
var result = {head: this, tail: ""};
|
||||
if (!limit || limit < 1)
|
||||
return result;
|
||||
if (!delimiter || delimiter =="")
|
||||
result.head= this.substring(0,limit);
|
||||
else {
|
||||
var re = new RegExp ("(" + delimiter + "+)");
|
||||
result.head = this.split(re,2*limit-1).join("");
|
||||
}
|
||||
if (result.head != this) {
|
||||
result.tail = this.substring(result.head.length).trim();
|
||||
if (result.tail) {
|
||||
if (clipping == null)
|
||||
clipping = "...";
|
||||
result.head = result.head.trim()+clipping;
|
||||
result.tail = clipping + result.tail;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* get the head of a string
|
||||
* @see String.prototype.embody()
|
||||
*/
|
||||
String.prototype.head = function(limit, clipping, delimiter) {
|
||||
return this.embody(limit, clipping, delimiter).head;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* get the tail of a string
|
||||
* @see String.prototype.embody()
|
||||
*/
|
||||
String.prototype.tail = function(limit, clipping, delimiter) {
|
||||
return this.embody(limit, clipping, delimiter).tail;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* set clip method out of compatibility/convenience reason
|
||||
* FIXME: we eventually have to get rid of this one...
|
||||
* @see String.prototype.head()
|
||||
*/
|
||||
String.prototype.clip = String.prototype.head;
|
||||
|
||||
|
||||
/**
|
||||
* function inserts a string every number of characters
|
||||
* @param Int number of characters after which insertion should take place
|
||||
* @param String string to be inserted
|
||||
* @param Boolean definitely insert at each interval position
|
||||
* @return String resulting string
|
||||
*/
|
||||
String.prototype.group = function(interval, str, ignoreWhiteSpace) {
|
||||
if (!interval || interval < 1)
|
||||
interval = 20;
|
||||
if (!str || this.length < interval)
|
||||
return this;
|
||||
res.push();
|
||||
for (var i=0; i<this.length; i=i+interval) {
|
||||
var strPart = this.substring(i, i+interval);
|
||||
res.write(strPart);
|
||||
if (ignoreWhiteSpace == true ||
|
||||
(strPart.length == interval && !/\s/g.test(strPart))) {
|
||||
res.write(str);
|
||||
}
|
||||
}
|
||||
return res.pop();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* replace all linebreaks and optionally all w/br tags
|
||||
* @param Boolean flag indicating if html tags should be replaced
|
||||
* @param String replacement for the linebreaks / html tags
|
||||
* @return String the unwrapped string
|
||||
*/
|
||||
String.prototype.unwrap = function(removeTags, replacement) {
|
||||
if (replacement == null)
|
||||
replacement = "";
|
||||
var str = this.replace(/[\n|\r]/g, replacement);
|
||||
if (removeTags)
|
||||
str = str.replace(/<[w]?br *\/?>/g, replacement);
|
||||
return str;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function calculates the md5 hash of a string
|
||||
* @return String md5 hash of the string
|
||||
*/
|
||||
String.prototype.md5 = function() {
|
||||
return Packages.helma.util.MD5Encoder.encode(this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function repeats a string passed as argument
|
||||
* @param Int amount of repetitions
|
||||
* @return String resulting string
|
||||
*/
|
||||
String.prototype.repeat = function(multiplier) {
|
||||
res.push();
|
||||
for (var i=0; i<multiplier; i++)
|
||||
res.write(this);
|
||||
return res.pop();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function returns true if the string starts with
|
||||
* the string passed as argument
|
||||
* @param String string pattern to search for
|
||||
* @return Boolean true in case it matches the beginning
|
||||
* of the string, false otherwise
|
||||
*/
|
||||
String.prototype.startsWith = function(str, offset) {
|
||||
var javaObj = new java.lang.String(this);
|
||||
if (offset != null)
|
||||
return javaObj.startsWith(str, offset);
|
||||
return javaObj.startsWith(str);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function returns true if the string ends with
|
||||
* the string passed as argument
|
||||
* @param String string pattern to search for
|
||||
* @return Boolean true in case it matches the end of
|
||||
* the string, false otherwise
|
||||
*/
|
||||
String.prototype.endsWith = function(str) {
|
||||
var javaObj = new java.lang.String(this);
|
||||
return javaObj.endsWith(str);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* fills a string with another string up to a desired length
|
||||
* @param String the filling string
|
||||
* @param Number the desired length of the resulting string
|
||||
* @param Number the direction which the string will be padded in:
|
||||
* -1: left 0: both (balance) 1: right
|
||||
* (you can use the constants String.LEFT,
|
||||
* String.BALANCE and String.RIGHT here as well.)
|
||||
* @return String the resulting string
|
||||
*/
|
||||
String.prototype.pad = function(str, len, mode) {
|
||||
if (str == null || len == null)
|
||||
return this;
|
||||
var diff = len - this.length;
|
||||
if (diff == 0)
|
||||
return this;
|
||||
var left, right = 0;
|
||||
if (mode == null || mode == String.RIGHT)
|
||||
right = diff;
|
||||
else if (mode == String.LEFT)
|
||||
left = diff;
|
||||
else if (mode == String.BALANCE) {
|
||||
right = Math.round(diff / 2);
|
||||
left = diff - right;
|
||||
}
|
||||
res.push();
|
||||
for (var i=0; i<left; i++)
|
||||
res.write(str);
|
||||
res.write(this);
|
||||
for (var i=0; i<right; i++)
|
||||
res.write(str);
|
||||
return res.pop();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function returns true if a string contains the string
|
||||
* passed as argument
|
||||
* @param String string to search for
|
||||
* @param Int Position to start search
|
||||
* @param Boolean
|
||||
*/
|
||||
String.prototype.contains = function(str, fromIndex) {
|
||||
if (this.indexOf(str, fromIndex ? fromIndex : 0) > -1)
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* function compares a string with the one passed as argument
|
||||
* using diff
|
||||
* @param String String to compare against String object value
|
||||
* @param String Optional regular expression string to use for
|
||||
* splitting. If not defined, newlines will be used.
|
||||
* @return Object Array containing one JS object for each line
|
||||
* with the following properties:
|
||||
* .num Line number
|
||||
* .value String line if unchanged
|
||||
* .deleted Obj Array containing deleted lines
|
||||
* .inserted Obj Array containing added lines
|
||||
*/
|
||||
String.prototype.diff = function(mod, separator) {
|
||||
// if no separator use line separator
|
||||
var regexp = (typeof(separator) == "undefined") ?
|
||||
new RegExp("\r\n|\r|\n") :
|
||||
new RegExp(separator);
|
||||
// split both strings into arrays
|
||||
var orig = this.split(regexp);
|
||||
var mod = mod.split(regexp);
|
||||
// create the Diff object
|
||||
var diff = new Packages.helma.util.Diff(orig, mod);
|
||||
// get the diff.
|
||||
var d = diff.diff();
|
||||
if (!d)
|
||||
return null;
|
||||
|
||||
var max = Math.max(orig.length, mod.length);
|
||||
var result = new Array();
|
||||
for (var i=0;i<max;i++) {
|
||||
var line = result[i];
|
||||
if (!line) {
|
||||
line = new Object();
|
||||
line.num = (i+1);
|
||||
result[i] = line;
|
||||
}
|
||||
if (d && i == d.line1) {
|
||||
if (d.deleted) {
|
||||
var del = new Array();
|
||||
for (var j=d.line0; j<d.line0+d.deleted; j++)
|
||||
del[del.length] = orig[j];
|
||||
line.deleted = del;
|
||||
}
|
||||
if (d.inserted) {
|
||||
var ins = new Array();
|
||||
for (var j=d.line1; j<d.line1+d.inserted; j++)
|
||||
ins[ins.length] = mod[j];
|
||||
line.inserted = ins;
|
||||
}
|
||||
i = d.line1 + d.inserted -1;
|
||||
d = d.link;
|
||||
} else {
|
||||
line.value = mod[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* remove leading and trailing whitespace
|
||||
*/
|
||||
String.prototype.trim = function () {
|
||||
var s = new java.lang.String(this);
|
||||
return String(s.trim());
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* returns true if the string looks like an e-mail
|
||||
*/
|
||||
String.prototype.isEmail = function() {
|
||||
return String.EMAILPATTERN.test(this);
|
||||
};
|
||||
|
||||
|
||||
// wrapper methods for string-related
|
||||
// global helma functions
|
||||
|
||||
String.prototype.encode = function() {
|
||||
return encode(this);
|
||||
};
|
||||
|
||||
String.prototype.encodeXml = function() {
|
||||
return encodeXml(this);
|
||||
};
|
||||
|
||||
String.prototype.encodeForm = function() {
|
||||
return encodeForm(this);
|
||||
};
|
||||
|
||||
String.prototype.format = function() {
|
||||
return format(this);
|
||||
};
|
||||
|
||||
String.prototype.stripTags = function() {
|
||||
return stripTags(this);
|
||||
};
|
||||
|
||||
|
||||
// prevent any newly added properties from being enumerated
|
||||
for (var i in String)
|
||||
String.dontEnum(i);
|
||||
for (var i in String.prototype)
|
||||
String.prototype.dontEnum(i);
|
Loading…
Add table
Add a link
Reference in a new issue