Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
taroMeeting
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fengzhaoyu
taroMeeting
Commits
832d70bc
Commit
832d70bc
authored
6 years ago
by
王树冬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add serialize
parent
48c6eafa
master
No related merge requests found
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
0 deletions
+66
-0
serialize.js
src/utils/serialize.js
+66
-0
No files found.
src/utils/serialize.js
0 → 100644
View file @
832d70bc
export
const
indexOf
=
(
haystack
,
needle
)
=>
{
for
(
var
i
=
0
;
i
<
haystack
.
length
;
++
i
)
{
if
(
haystack
[
i
]
===
needle
)
return
i
;
}
return
-
1
;
}
export
const
stringify
=
(
obj
,
replacer
,
spaces
,
cycleReplacer
)
=>
{
return
JSON
.
stringify
(
obj
,
serializer
(
replacer
,
cycleReplacer
),
spaces
);
}
export
const
stringifyError
=
(
value
)
=>
{
var
err
=
{
stack
:
value
.
stack
,
message
:
value
.
message
,
name
:
value
.
name
};
for
(
var
i
in
value
)
{
if
(
Object
.
prototype
.
hasOwnProperty
.
call
(
value
,
i
))
{
err
[
i
]
=
value
[
i
];
}
}
return
err
;
}
export
const
serializer
=
(
replacer
,
cycleReplacer
)
=>
{
var
stack
=
[];
var
keys
=
[];
if
(
cycleReplacer
==
null
)
{
cycleReplacer
=
function
(
key
,
value
)
{
if
(
stack
[
0
]
===
value
)
{
return
'[Circular ~]'
;
}
return
'[Circular ~.'
+
keys
.
slice
(
0
,
indexOf
(
stack
,
value
)).
join
(
'.'
)
+
']'
;
};
}
return
function
(
key
,
value
)
{
if
(
stack
.
length
>
0
)
{
var
thisPos
=
indexOf
(
stack
,
this
);
~
thisPos
?
stack
.
splice
(
thisPos
+
1
)
:
stack
.
push
(
this
);
~
thisPos
?
keys
.
splice
(
thisPos
,
Infinity
,
key
)
:
keys
.
push
(
key
);
if
(
~
indexOf
(
stack
,
value
))
{
value
=
cycleReplacer
.
call
(
this
,
key
,
value
);
}
}
else
{
stack
.
push
(
value
);
}
return
replacer
==
null
?
value
instanceof
Error
?
stringifyError
(
value
)
:
value
:
replacer
.
call
(
this
,
key
,
value
);
};
}
// try {
// throw new Error("Whoops!");
// } catch (e) {
// e.name = {stark:'wang',hehe:{hh:'111'}}
// let ee = stringify(e)
// console.log(typeof ee)
// }
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment