• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jaubourg / wires / 3624033844

pending completion
3624033844

push

github

GitHub
build(deps-dev): bump eslint from 8.16.0 to 8.29.0

289 of 306 branches covered (94.44%)

526 of 537 relevant lines covered (97.95%)

1805.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/lib/serialize-javascript/utils.js
1
/*
2
copyright (c) 2014, Yahoo! Inc. All rights reserved
3
copyrights licensed under the New BSD License
4
see the accompanying LICENSE file for terms
5
copyright (c) 2022, Julian Aubourg All rights reserved
6
copyrights licensed under the MIT License
7
see the accompanying LICENSE file for terms
8
*/
9

10
"use strict";
11

12
const isObject = require( `../isObject` );
28✔
13

14
const rArrowFunction = /.*?=>.*?/;
28✔
15
const rNative = /\{\s*\[native code\]\s*\}/g;
28✔
16
const rPureFunction = /function.*?\(/;
28✔
17
const rUnsafeChars = /[<>/\u2028\u2029]/g;
28✔
18

19
// mapping of unsafe HTML and invalid JavaScript line terminator chars to their
20
// unicode char counterparts which are safe to use in JavaScript strings.
21
const ESCAPED_CHARS = {
28✔
22
    "<": `\\u003C`,
23
    ">": `\\u003E`,
24
    "/": `\\u002F`,
25
    "\u2028": `\\u2028`,
26
    "\u2029": `\\u2029`,
27
};
28

29
const RESERVED_SYMBOLS = [ `*`, `async` ];
28✔
30

31
const deleteFunctions = object => (
28✔
32
    isObject( object ) ?
8,000✔
33
        Object.fromEntries( Object.entries( object ).filter( ( [ , value ] ) => ( typeof value !== `function` ) ) ) :
2,000✔
34
        object
35
);
36

37
const escapeUnsafeChars = str => str.replace( rUnsafeChars, unsafeChar => ESCAPED_CHARS[ unsafeChar ] );
15,284✔
38

39
const identity = x => x;
3,420✔
40

41
const serializeFunction = fn => {
28✔
42
    const serializedFn = fn.toString();
6,029✔
43
    if ( rNative.test( serializedFn ) ) {
6,029✔
44
        throw new TypeError( `Serializing native function: { fn.name }` );
1✔
45
    }
46

47
    // pure functions, example: {key: function() {}}
48
    if ( rPureFunction.test( serializedFn ) ) {
6,028✔
49
        return serializedFn;
5✔
50
    }
51

52
    // arrow functions, example: arg1 => arg1+5
53
    if ( rArrowFunction.test( serializedFn ) ) {
6,023✔
54
        return serializedFn;
6,022✔
55
    }
56

57
    const argsStartsAt = serializedFn.indexOf( `(` );
1✔
58
    const def = serializedFn.substr( 0, argsStartsAt )
1✔
59
        .trim()
60
        .split( ` ` )
61
        .filter( val => ( val.length > 0 ) );
1✔
62

63
    const nonReservedSymbols = def.filter( val => RESERVED_SYMBOLS.indexOf( val ) === -1 );
1✔
64

65
    // enhanced literal objects, example: {key() {}}
66
    if ( nonReservedSymbols.length > 0 ) {
1!
67
        return `${ def.indexOf( `async` ) > -1 ? `async ` : `` }function${
1!
68
            def.join( `` ).indexOf( `*` ) > -1 ? `*` : ``
1!
69
        }${
70
            serializedFn.substr( argsStartsAt )
71
        }`;
72
    }
73

74
    // arrow functions
75
    return serializedFn;
×
76
};
77

78
module.exports = {
28✔
79
    deleteFunctions,
80
    escapeUnsafeChars,
81
    identity,
82
    serializeFunction,
83
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc