-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (35 loc) · 1.32 KB
/
app.js
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
"use strict";
/**
* @n number required - max length
* @val string|integer required - value used
* @a string optional - char to be repeated
* @b true|false optional - slicing direction
*/
exports.left = function (n, val, a, b) {
val = (!val && val !== 0) ? '' : val + '';
a = (!a && a !== 0) ? ' ' : String(a);
b = (typeof b === 'boolean') ? b : true;
var len = val.length;
if (len == n) return val;
if (len > n) return (b === true) ? val.slice(0, n) : val.slice(len - n, len);
var str = a.repeat(n - len) + val;
var strLen = str.length;
return (strLen > n) ? ((b === true) ? str.slice(0, n) : str.slice(strLen - n, strLen)) : str.slice(strLen - n, strLen);
};
/**
* @n number required - max length
* @val string|integer required - value used
* @a string optional - char to be repeated
* @b true|false optional - slicing direction
*/
exports.right = function (n, val, a, b) {
val = (!val && val !== 0) ? '' : val + '';
a = (!a && a !== 0) ? ' ' : String(a);
b = (typeof b === 'boolean') ? b : true;
var len = val.length;
if (len == n) return val;
if (len > n) return (b === true) ? val.slice(0, n) : val.slice(len - n, len);
var str = val + a.repeat(n - len);
var strLen = str.length;
return (strLen > n) ? ((b === true) ? str.slice(0, n) : str.slice(strLen - n, strLen)) : str.slice(0, n);
};