locutus 2.0.35 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locutus",
3
- "version": "2.0.35",
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
+ }