Namespace glow.util
Defined in: core.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
Core JavaScript helpers
|
| Method Attributes | Method Name and Description |
|---|---|
| <static> |
glow.util.apply(destination, source)
Copies properties from one object to another
All properties from 'source' will be copied onto
'destination', potentially overwriting existing properties
on 'destination'.
|
| <static> |
glow.util.decodeJson(string, opts)
Decodes a string JSON representation into an object.
|
| <static> |
glow.util.decodeUrl(string)
Decodes a query string into an object.
|
| <static> |
glow.util.encodeJson(object)
Encodes an object into a string JSON representation.
|
| <static> |
glow.util.encodeUrl(object)
Encodes an object for use as a query string.
|
| <static> |
glow.util.escapeRegex(str)
Escape special regex chars from a string
|
| <static> |
glow.util.extend(sub, base, additionalProperties)
Copies the prototype of one object to another
The 'subclass' can also access the 'base class' via subclass.base
|
| <static> |
glow.util.getType(object)
Get the native type or constructor name of an object.
|
| <static> |
glow.util.interpolate(template, data, opts)
Replaces placeholders in a string with data from an object
|
| <static> |
glow.util.trim(str)
Removes leading and trailing whitespace from a string
|
Method Detail
<static>
{Object}
glow.util.apply(destination, source)
Copies properties from one object to another
All properties from 'source' will be copied onto
'destination', potentially overwriting existing properties
on 'destination'.
Properties from 'source's prototype chain will not be copied.
var obj = glow.util.apply({foo: "hello", bar: "world"}, {bar: "everyone"});
//results in {foo: "hello", bar: "everyone"}
- Parameters:
- {Object} destination Optional
- Destination object. If this object is undefined or falsey, a new object will be created.
- {Object} source Optional
- Properties of this object will be copied onto the destination If this object is undefined or falsey, a new object will be created.
- Returns:
- {Object} The destination object.
<static>
{Object}
glow.util.decodeJson(string, opts)
Decodes a string JSON representation into an object.
Returns a JavaScript object that mirrors the data given.
var getRef = glow.util.decodeJson('{foo: "Foo", bar: ["Bar 1", "Bar2"]}');
// will return {foo: "Foo", bar: ["Bar 1", "Bar2"]}
var getRef = glow.util.decodeJson('foobar', {safeMode: true});
// will throw an error
- Parameters:
- {String} string
- The string to be decoded. Must be valid JSON.
- {Object} opts
- Zero or more of the following as properties of an object:
- {Boolean} opts.safeMode Optional, Default: false
- Whether the string should only be decoded if it is deemed "safe". The json.org regular expression checks are used.
- Returns:
- {Object}
<static>
{Object}
glow.util.decodeUrl(string)
Decodes a query string into an object.
Returns an object representing the data given by the query
string, with all values suitably unescaped. All keys in the
query string are keys of the object. Repeated keys result
in an array.
var getRef = glow.util.decodeUrl("foo=Foo&bar=Bar%201&bar=Bar2");
// will return the object {foo: "Foo", bar: ["Bar 1", "Bar2"]}
- Parameters:
- {String} string
- The query string to be decoded. It should not include the initial question mark.
- Returns:
- {Object}
<static>
{Object}
glow.util.encodeJson(object)
Encodes an object into a string JSON representation.
Returns a string representing the object as JSON.
var myObj = {foo: "Foo", bar: ["Bar 1", "Bar2"]};
var getRef = glow.util.encodeJson(myObj);
// will return '{"foo": "Foo", "bar": ["Bar 1", "Bar2"]}'
- Parameters:
- {Object} object
- The object to be encoded. This can be arbitrarily nested, but must not contain functions or cyclical structures.
- Returns:
- {Object}
<static>
{String}
glow.util.encodeUrl(object)
Encodes an object for use as a query string.
Returns a string representing the object suitable for use
as a query string, with all values suitably escaped.
It does not include the initial question mark. Where the
input field was an array, the key is repeated in the output.
var getRef = glow.util.encodeUrl({foo: "Foo", bar: ["Bar 1", "Bar2"]});
// will return "foo=Foo&bar=Bar%201&bar=Bar2"
- Parameters:
- {Object} object
- The object to be encoded. This must be a hash whose values can only be primitives or arrays of primitives.
- Returns:
- {String}
<static>
{string}
glow.util.escapeRegex(str)
Escape special regex chars from a string
var str = glow.util.escapeRegex('[Hello. Is this escaped?]');
// Outputs:
// \[Hello\. Is this escaped\?\]
- Parameters:
- {string} str
- String to escape
- Returns:
- {string} Escaped string
<static>
glow.util.extend(sub, base, additionalProperties)
Copies the prototype of one object to another
The 'subclass' can also access the 'base class' via subclass.base
function MyClass(arg) {
this.prop = arg;
}
MyClass.prototype.showProp = function() {
alert(this.prop);
};
function MyOtherClass(arg) {
//call the base class's constructor
MyOtherClass.base.apply(this, arguments);
}
glow.util.extend(MyOtherClass, MyClass, {
setProp: function(newProp) {
this.prop = newProp;
}
});
var test = new MyOtherClass("hello");
test.showProp(); // alerts "hello"
test.setProp("world");
test.showProp(); // alerts "world"
- Parameters:
- {Function} sub
- Class which inherits properties.
- {Function} base
- Class to inherit from.
- {Object} additionalProperties
- An object of properties and methods to add to the subclass.
<static>
glow.util.getType(object)
Get the native type or constructor name of an object.
This allows you to safely get the type of an object, even
if it came from another frame.
glow.util.getType( null ); // 'null'
glow.util.getType( undefined ); // 'undefined'
glow.util.getType('Hello'); // 'string'
glow.util.getType( {} ); // 'Object'
glow.util.getType(12); // 'number'
glow.util.getType( [] ); // 'Array'
glow.util.getType( function(){} ); // 'Function'
glow.util.getType( glow('#whatever') ); // 'NodeList'
var MyConstructor = function() {},
obj = new MyConstructor;
glow.util.getType(obj); // ''
// The above returns an empty string as the constructor
// is an anonymous function and therefore has no name
- Parameters:
- {Object} object
- Object to get the type of.
<static>
{String}
glow.util.interpolate(template, data, opts)
Replaces placeholders in a string with data from an object
var data = {
name: "Domino",
colours: ["black", "white"],
family: {
mum: "Spot",
dad: "Patch",
siblings: []
}
};
var template = "My cat's name is {name}. His colours are {colours.0} & {colours.1}. His mum is {family.mum}, his dad is {family.dad} and he has {family.siblings.length} brothers or sisters.";
var result = glow.util.interpolate(template, data);
// result == "My cat's name is Domino. His colours are black & white. His mum is Spot, his dad is Patch and he has 0 brothers or sisters."
var data = {
name: 'Haxors!!1 '
}
var template = 'Hello, my name is {name}
';
var result = glow.util.interpolate(template, data, {
escapeHtml: true
});
// result == 'Hello, my name is Haxors!!1 <script src="hackhackhack.js"></script>
'
- Parameters:
- {String} template
- The string containing {placeholders}
- {Object} data
- Object containing the data to be merged in to the template
The object can contain nested data objects and arrays, with nested object properties and array elements are accessed using dot notation. eg foo.bar or foo.0.
The data labels in the object cannot contain characters used in the template delimiters, so if the data must be allowed to contain the default { and } delimiters, the delimters must be changed using the option below.
- {Object} opts
- Options object
- {String} opts.delimiter Optional, Default: "{}"
- Alternative label delimiter(s) for the template The first character supplied will be the opening delimiter, and the second the closing. If only one character is supplied, it will be used for both ends.
- {Boolean} opts.escapeHtml Optional, Default: false
- Escape any special html characters found in the data object Use this to safely inject data from the user into an HTML template. The glow.dom module must be present for this feature to work (an error will be thrown otherwise).
- Returns:
- {String}
<static>
{String}
glow.util.trim(str)
Removes leading and trailing whitespace from a string
glow.util.trim(" Hello World "); // "Hello World"
- Parameters:
- {string} str
- String to trim
- Returns:
- {String} String without leading and trailing whitespace