locutus 2.0.34 → 2.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
1
  module.exports.int = require('./int')
2
2
  module.exports.length = require('./length')
3
3
  module.exports.substr = require('./substr')
4
+ module.exports.tolower = require('./tolower')
5
+ module.exports.toupper = require('./toupper')
@@ -0,0 +1,11 @@
1
+ module.exports = function tolower(s) {
2
+ // discuss at: https://locutus.io/awk/tolower/
3
+ // parity verified: GNU AWK 5.3
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // example 1: tolower('HELLO')
6
+ // returns 1: 'hello'
7
+ // example 2: tolower('Hello World')
8
+ // returns 2: 'hello world'
9
+
10
+ return String(s).toLowerCase()
11
+ }
@@ -0,0 +1,11 @@
1
+ module.exports = function toupper(s) {
2
+ // discuss at: https://locutus.io/awk/toupper/
3
+ // parity verified: GNU AWK 5.3
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // example 1: toupper('hello')
6
+ // returns 1: 'HELLO'
7
+ // example 2: toupper('Hello World')
8
+ // returns 2: 'HELLO WORLD'
9
+
10
+ return String(s).toUpperCase()
11
+ }
@@ -0,0 +1,3 @@
1
+ module.exports.lower = require('./lower')
2
+ module.exports.sub = require('./sub')
3
+ module.exports.upper = require('./upper')
@@ -0,0 +1,11 @@
1
+ module.exports = function lower(s) {
2
+ // discuss at: https://locutus.io/lua/lower/
3
+ // parity verified: Lua 5.4
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // example 1: lower('HELLO')
6
+ // returns 1: 'hello'
7
+ // example 2: lower('Hello World')
8
+ // returns 2: 'hello world'
9
+
10
+ return String(s).toLowerCase()
11
+ }
@@ -0,0 +1,33 @@
1
+ module.exports = function sub(s, i, j) {
2
+ // discuss at: https://locutus.io/lua/sub/
3
+ // parity verified: Lua 5.4
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // note 1: Lua uses 1-based indexing, negative indices count from end
6
+ // example 1: sub('Hello', 1, 3)
7
+ // returns 1: 'Hel'
8
+ // example 2: sub('Hello', 2)
9
+ // returns 2: 'ello'
10
+ // example 3: sub('Hello', -2)
11
+ // returns 3: 'lo'
12
+
13
+ s = String(s)
14
+ const len = s.length
15
+
16
+ // Convert Lua 1-based index to JS 0-based
17
+ // Negative indices count from end in Lua
18
+ let start = i < 0 ? len + i : i - 1
19
+ let end = j === undefined ? len : j < 0 ? len + j + 1 : j
20
+
21
+ // Clamp to valid range
22
+ if (start < 0) {
23
+ start = 0
24
+ }
25
+ if (end > len) {
26
+ end = len
27
+ }
28
+ if (start >= end) {
29
+ return ''
30
+ }
31
+
32
+ return s.substring(start, end)
33
+ }
@@ -0,0 +1,11 @@
1
+ module.exports = function upper(s) {
2
+ // discuss at: https://locutus.io/lua/upper/
3
+ // parity verified: Lua 5.4
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // example 1: upper('hello')
6
+ // returns 1: 'HELLO'
7
+ // example 2: upper('Hello World')
8
+ // returns 2: 'HELLO WORLD'
9
+
10
+ return String(s).toUpperCase()
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locutus",
3
- "version": "2.0.34",
3
+ "version": "2.0.36",
4
4
  "description": "Locutus other languages' standard libraries to JavaScript for fun and educational purposes",
5
5
  "keywords": [
6
6
  "php",
@@ -1 +1,4 @@
1
+ module.exports.lc = require('./lc')
1
2
  module.exports.length = require('./length')
3
+ module.exports.substr = require('./substr')
4
+ module.exports.uc = require('./uc')
@@ -0,0 +1,11 @@
1
+ module.exports = function lc(str) {
2
+ // discuss at: https://locutus.io/perl/lc/
3
+ // parity verified: Perl 5.40
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // example 1: lc('HELLO')
6
+ // returns 1: 'hello'
7
+ // example 2: lc('Hello World')
8
+ // returns 2: 'hello world'
9
+
10
+ return String(str).toLowerCase()
11
+ }
@@ -0,0 +1,44 @@
1
+ module.exports = function substr(str, offset, length) {
2
+ // discuss at: https://locutus.io/perl/substr/
3
+ // parity verified: Perl 5.40
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // note 1: Perl's offset is 0-based, negative counts from end
6
+ // example 1: substr('hello', 0, 3)
7
+ // returns 1: 'hel'
8
+ // example 2: substr('hello', 1)
9
+ // returns 2: 'ello'
10
+ // example 3: substr('hello', -2)
11
+ // returns 3: 'lo'
12
+
13
+ str = String(str)
14
+ const len = str.length
15
+
16
+ // Handle negative offset (count from end)
17
+ let start = offset < 0 ? len + offset : offset
18
+
19
+ // Clamp start to valid range
20
+ if (start < 0) {
21
+ start = 0
22
+ }
23
+ if (start > len) {
24
+ return ''
25
+ }
26
+
27
+ // If length is undefined, return rest of string
28
+ if (length === undefined) {
29
+ return str.substring(start)
30
+ }
31
+
32
+ // Handle negative length (leave that many chars at end)
33
+ let end
34
+ if (length < 0) {
35
+ end = len + length
36
+ if (end < start) {
37
+ return ''
38
+ }
39
+ } else {
40
+ end = start + length
41
+ }
42
+
43
+ return str.substring(start, end)
44
+ }
@@ -0,0 +1,11 @@
1
+ module.exports = function uc(str) {
2
+ // discuss at: https://locutus.io/perl/uc/
3
+ // parity verified: Perl 5.40
4
+ // original by: Kevin van Zonneveld (https://kvz.io)
5
+ // example 1: uc('hello')
6
+ // returns 1: 'HELLO'
7
+ // example 2: uc('Hello World')
8
+ // returns 2: 'HELLO WORLD'
9
+
10
+ return String(str).toUpperCase()
11
+ }