-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalar.js
More file actions
76 lines (57 loc) · 1.67 KB
/
scalar.js
File metadata and controls
76 lines (57 loc) · 1.67 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/javascript/blob/master/LICENSE
*/
// scalar
// script with functions related to scalar values
// import
import { Arr, Bool, Json, Integer, Num } from '../index.js';
// export
export default {
// is
// retourne vrai si la valeur est scalar
is: function(value)
{
let r = false;
const type = typeof value;
if(type === 'boolean' || type === 'number' || type === 'string')
r = true;
return r;
},
// isNotBool
// retourne vrai si scalar mais pas bool
isNotBool: function(value)
{
return this.is(value) && !Bool.is(value);
},
// cast
// permet de cast une valeur selon un type donné en deuxième argument
cast: function(r,type)
{
if(r != null && type != null)
{
if(Arr.in(type,[true,'json']))
r = Json.decode(r);
else if(type === 'int')
r = Integer.cast(r);
else if(type === 'num')
r = Num.cast(r);
else if(type === 'bool' && this.is(r))
r = this.toBool(r);
}
return r;
},
// toBool
// retourne un booléean à partir d'un scalar
toBool: function(value)
{
let r = null;
this.typecheck(value);
if(Arr.in(value,[1,'1',true,'true']))
r = true;
else if(Arr.in(value,[0,'0',false,'false']))
r = false;
return r;
}
}