-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstr.js
More file actions
51 lines (38 loc) · 1.03 KB
/
str.js
File metadata and controls
51 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
var getStr = require('./util').getStr;
module.exports = {
concat: strConcat,
slice: strSlice,
pos: strPos,
lower: strLower,
upper: strUpper
};
function strConcat(args) {
var result = '';
for (var i=0; i<args.length; i++)
result += getStr(args, i, 'concat');
return result;
}
function strSlice(args) {
var str = getStr(args, 0, 'slice');
var begin = args[1];
var end = args[2];
if (typeof begin != 'number' || (typeof end != 'number' && typeof end != 'undefined'))
throw new Error('operation slice expects numbers for the begin and end indices');
return str.slice(begin, end);
}
function strPos(args) {
var str = getStr(args, 0, 'pos');
var substr = getStr(args, 1, 'pos');
return str.indexOf(substr);
}
function strLower(arg) {
if (typeof arg != 'string')
throw new Error('operation lower expects string');
return arg.toLowerCase();
}
function strUpper(arg) {
if (typeof arg != 'string')
throw new Error('operation upper expects string');
return arg.toUpperCase();
}