hlp 3.6.3 → 3.6.5
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/README.md +60 -47
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
## motivation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
this is a lightweight javascript utility library that provides essential helpers for everyday coding tasks. it offers intuitive methods for variable existence checks, type validation, string manipulation, date operations, and more. designed with simplicity in mind, `hlp` eliminates boilerplate code and makes common programming patterns more concise and readable, allowing developers to focus on building features rather than writing repetitive utility code.
|
|
8
8
|
|
|
9
9
|
## installation
|
|
10
10
|
|
|
@@ -41,6 +41,14 @@ hlp.v( vrbl, 'default' )
|
|
|
41
41
|
// get first variable that exists, otherwise ''
|
|
42
42
|
hlp.v( vrbl1, vrbl2, vrbl3 )
|
|
43
43
|
|
|
44
|
+
// truthness
|
|
45
|
+
if( hlp.true(vrbl) ) {}
|
|
46
|
+
if( hlp.false(vrbl) ) {}
|
|
47
|
+
|
|
48
|
+
// be aware, that hlp.true is not always the logic negation of hlp.false
|
|
49
|
+
hlp.true(null) // false
|
|
50
|
+
hlp.false(null) // false
|
|
51
|
+
|
|
44
52
|
// loop over arrays/objects only if possible
|
|
45
53
|
hlp.loop(['foo','bar','baz'], (vrbl__value) => {});
|
|
46
54
|
hlp.loop(['foo','bar','baz'], (vrbl__value, vrbl__key) => {});
|
|
@@ -49,6 +57,19 @@ hlp.loop([], (vrbl__value, vrbl__key) => { }) // does nothing
|
|
|
49
57
|
hlp.loop({}, (vrbl__value, vrbl__key) => { }) // does nothing
|
|
50
58
|
hlp.loop(null, (vrbl__value, vrbl__key) => { }) // does nothing
|
|
51
59
|
|
|
60
|
+
// if you are unsure, if a variable is even set before checking its existence,
|
|
61
|
+
// simply put it inside this helper function
|
|
62
|
+
// that works because javascript only evaluates the content
|
|
63
|
+
// of the inner callback (or closure) when it is actually executed.
|
|
64
|
+
if( hlp.x(() => vrbl) )
|
|
65
|
+
if( hlp.nx(() => vrbl) )
|
|
66
|
+
if( hlp.true(() => vrbl) )
|
|
67
|
+
if( hlp.false(() => vrbl) )
|
|
68
|
+
if( hlp.v(() => vrbl) === 'foo' )
|
|
69
|
+
if( hlp.v(() => vrbl) == 1337 )
|
|
70
|
+
hlp.v(() => vrbl)
|
|
71
|
+
hlp.loop((() => vrbl), (vrbl__value, vrbl__key) => { })
|
|
72
|
+
|
|
52
73
|
// capitalize
|
|
53
74
|
hlp.capitalize('foo') // Foo
|
|
54
75
|
|
|
@@ -598,20 +619,43 @@ hlp.animate(
|
|
|
598
619
|
).then(() => { console.log('done'); });
|
|
599
620
|
```
|
|
600
621
|
|
|
601
|
-
##
|
|
622
|
+
## php implementation
|
|
623
|
+
|
|
624
|
+
there is also a php implemenation [stringhelper](https://github.com/vielhuber/stringhelper) with similiar functions available.
|
|
625
|
+
|
|
626
|
+
## testing
|
|
627
|
+
|
|
628
|
+
`npm run js:tests`
|
|
629
|
+
|
|
630
|
+
## recommendations
|
|
631
|
+
|
|
632
|
+
### existence
|
|
633
|
+
|
|
634
|
+
```js
|
|
635
|
+
if (foo) {} // ⚠️ be aware of: '0'/[]/{}
|
|
636
|
+
if (foo?.prop) {}
|
|
637
|
+
if (foo?.someFun1()?.someFun2()?.getName()) {}
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
### truthness
|
|
641
|
+
|
|
642
|
+
```js
|
|
643
|
+
if (foo === true) {}
|
|
644
|
+
if (foo?.prop === true) {}
|
|
645
|
+
if (foo?.someFun1()?.someFun2()?.getName() === true) {}
|
|
646
|
+
```
|
|
602
647
|
|
|
603
|
-
###
|
|
648
|
+
### comparison
|
|
604
649
|
|
|
605
650
|
```js
|
|
606
|
-
if
|
|
607
|
-
if (
|
|
608
|
-
|
|
651
|
+
if (foo === 'foo') {}
|
|
652
|
+
if (foo?.prop === 'foo') {}
|
|
653
|
+
if (foo?.someFun1()?.someFun2()?.getName() === 'foo') {}
|
|
609
654
|
```
|
|
610
655
|
|
|
611
|
-
|
|
656
|
+
these recommendations ground on the fact, that js has a lot of pitfalls, when comparing loosely:
|
|
612
657
|
|
|
613
658
|
```js
|
|
614
|
-
// js has a lot of pitfalls, when comparing loosely
|
|
615
659
|
if( '' == [] ) // true
|
|
616
660
|
if( '' == [''] ) // true
|
|
617
661
|
if( '' == 0 ) // true
|
|
@@ -625,56 +669,25 @@ if( 0 == [] ) // true
|
|
|
625
669
|
if( 0 == [''] ) // true
|
|
626
670
|
if( [0] == false ) // true
|
|
627
671
|
if( [0] == 0 ) // true
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
also don't forget those delicacies:
|
|
628
675
|
|
|
629
|
-
|
|
676
|
+
```js
|
|
630
677
|
0 === -0 // true
|
|
631
678
|
NaN === NaN // false
|
|
632
679
|
(![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]] === 'fail' // true
|
|
680
|
+
```
|
|
633
681
|
|
|
634
|
-
|
|
682
|
+
this non-strict equality is symmetric, but not transitive:
|
|
683
|
+
|
|
684
|
+
```js
|
|
635
685
|
a = ''; b = 0; c = [0];
|
|
636
686
|
a == b; // true
|
|
637
687
|
b == c; // true
|
|
638
688
|
c == a; // false
|
|
639
|
-
|
|
640
|
-
// to overcome this issue, we...
|
|
641
|
-
|
|
642
|
-
// ...use strict comparison when possible
|
|
643
|
-
if( vrbl === 'foo' ) {}
|
|
644
|
-
|
|
645
|
-
// ...use loose comparison when appropriate
|
|
646
|
-
if( hlp.getParam('number') == 1337 ) {}
|
|
647
|
-
|
|
648
|
-
// ...check for truthness / falsiness with these helper methods
|
|
649
|
-
if( hlp.true(vrbl) ) {}
|
|
650
|
-
|
|
651
|
-
if( hlp.false(vrbl) ) {}
|
|
652
|
-
|
|
653
|
-
// be aware, that hlp.true is not always the logic negation of hlp.false
|
|
654
|
-
hlp.true(null) // false
|
|
655
|
-
hlp.false(null) // false
|
|
656
|
-
```
|
|
657
|
-
|
|
658
|
-
### try
|
|
659
|
-
|
|
660
|
-
```js
|
|
661
|
-
// if you are unsure, if a variable is even set before checking its existence,
|
|
662
|
-
// simply put it inside this helper function
|
|
663
|
-
if( hlp.x(() => vrbl) )
|
|
664
|
-
if( hlp.nx(() => vrbl) )
|
|
665
|
-
if( hlp.true(() => vrbl) )
|
|
666
|
-
if( hlp.false(() => vrbl) )
|
|
667
|
-
if( hlp.v(() => vrbl) === 'foo' )
|
|
668
|
-
if( hlp.v(() => vrbl) == 1337 )
|
|
669
|
-
echo hlp.v(() => vrbl)
|
|
670
|
-
// that works because javascript only evaluates the content of the inner callback (or closure) when it is actually executed.
|
|
671
|
-
hlp.loop((() => vrbl), (vrbl__value, vrbl__key) => { })
|
|
672
689
|
```
|
|
673
690
|
|
|
674
|
-
## php implementation
|
|
675
|
-
|
|
676
|
-
there is also a php implemenation [stringhelper](https://github.com/vielhuber/stringhelper) with similiar functions available.
|
|
677
|
-
|
|
678
691
|
## appendix
|
|
679
692
|
|
|
680
693
|
### existence matrix
|