Project

General

Profile

1
function is_undef(value) { return typeof value === 'undefined' }
2

    
3
function coalesce(value, empty) { return !is_undef(value) ? value : empty }
4

    
5
function starts_with(str, prefix)
6
{
7
	return str.substr(0, prefix.length) === prefix
8
}
9

    
10
function ends_with(str, suffix) { return str.substr(-suffix.length) === suffix }
11

    
12
function rm_prefix(prefix, str)
13
{
14
	return starts_with(str, prefix) ? str.substr(prefix.length) : str
15
}
16

    
17
function rm_suffix(suffix, str)
18
{
19
	return ends_with(str, suffix) ?
20
		str.substr(0, str.length - suffix.length) : str
21
}
22

    
23
function partition(str, delim)
24
{
25
	var delim_idx = str.indexOf(delim)
26
	return delim_idx >= 0 ? {head: str.substr(0, delim_idx),
27
		tail: str.substr(delim_idx+delim.length)} : {head: str, tail: ''}
28
}
29

    
30
function parse_dot_path(path) { return partition(path, '.') }
31

    
32
function parse_mixed_path(path)
33
{
34
	path = /^([\w-]*)(.*)$/.exec(path)
35
	return {head: path[1], tail: rm_prefix('.', path[2])}
36
}
37

    
38
function strip_url(url) { return rm_suffix('#', url) }
(12-12/13)