FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/utils/function.js at develop · joncv/mathjs · GitHub
joncv
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
src
/
utils
/
function.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (55 loc) · 1.97 KB
Breadcrumbs
mathjs
/
src
/
utils
/
function.js
Copy path
File metadata and controls
60 lines (55 loc) · 1.97 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
'use strict'
// function utils
/**
* Memoize a given function by caching the computed result.
* The cache of a memoized function can be cleared by deleting the `cache`
* property of the function.
*
*
@param
{
function
} fn The function to be memoized.
* Must be a pure function.
*
@param
{
function(args: Array)
} [hasher] A custom hash builder.
* Is JSON.stringify by default.
*
@return
{
function
} Returns the memoized function
*/
exports
.
memoize
=
function
(
fn
,
hasher
)
{
return
function
memoize
(
)
{
if
(
typeof
memoize
.
cache
!==
'object'
)
{
memoize
.
cache
=
{
}
}
const
args
=
[
]
for
(
let
i
=
0
;
i
<
arguments
.
length
;
i
++
)
{
args
[
i
]
=
arguments
[
i
]
}
const
hash
=
hasher
?
hasher
(
args
)
:
JSON
.
stringify
(
args
)
if
(
!
(
hash
in
memoize
.
cache
)
)
{
memoize
.
cache
[
hash
]
=
fn
.
apply
(
fn
,
args
)
}
return
memoize
.
cache
[
hash
]
}
}
/**
* Find the maximum number of arguments expected by a typed function.
*
@param
{
function
} fn A typed function
*
@return
{
number
} Returns the maximum number of expected arguments.
* Returns -1 when no signatures where found on the function.
*/
exports
.
maxArgumentCount
=
function
(
fn
)
{
return
Object
.
keys
(
fn
.
signatures
||
{
}
)
.
reduce
(
function
(
args
,
signature
)
{
const
count
=
(
signature
.
match
(
/
,
/
g
)
||
[
]
)
.
length
+
1
return
Math
.
max
(
args
,
count
)
}
,
-
1
)
}
/**
* Call a typed function with the
*
@param
{
function
} fn A function or typed function
*
@return
{
number
} Returns the maximum number of expected arguments.
* Returns -1 when no signatures where found on the function.
*/
exports
.
callWithRightArgumentCount
=
function
(
fn
,
args
,
argCount
)
{
return
Object
.
keys
(
fn
.
signatures
||
{
}
)
.
reduce
(
function
(
args
,
signature
)
{
const
count
=
(
signature
.
match
(
/
,
/
g
)
||
[
]
)
.
length
+
1
return
Math
.
max
(
args
,
count
)
}
,
-
1
)
}
Back
|
FazBrowse Home
|
New Git URL