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

jaubourg / wires / 3933987491

pending completion
3933987491

Pull #131

github

GitHub
Merge 1690b290f into 15fb4e2eb
Pull Request #131: build(deps-dev): bump eslint from 8.16.0 to 8.32.0

289 of 306 branches covered (94.44%)

526 of 537 relevant lines covered (97.95%)

1805.86 hits per line

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

98.21
/lib/serialize-javascript/Placeholders.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 { identity, serializeFunction } = require( `./utils` );
28✔
13

14
const rCollision = /^@[ABDFILMNRSTU]\d+@$/;
28✔
15
const rPlaceholder = /(\\)?"@([ABDFILMNRSTU])(\d+)@"/g;
28✔
16

17
const byPrototype = new Map( [
28✔
18
    [ Array, v => ( ( v.filter( () => true ).length !== v.length ) && `A` ) ],
12,113✔
19
    [ Date, `D` ],
20
    [ Map, `M` ],
21
    [ RegExp, `R` ],
22
    [ Set, `S` ],
23
    [ URL, `L` ],
24
].map( ( [ construct, code ] ) => [ construct.prototype, code ] ) );
168✔
25

26
const byType = new Map( [
28✔
27
    [ `bigint`, `B` ],
28
    [ `function`, `F` ],
29
    [ `number`, v => ( isNaN( v ) ? `N` : ( !isFinite( v ) && `I` ) ) ],
7,893✔
30
    [ `string`, v => ( rCollision.test( v ) && `T` ) ],
11,496✔
31
    [ `undefined`, `U` ],
32
] );
33

34
class Placeholders {
35
    #options;
36
    #serialize;
37
    #values;
38
    constructor( serialize, options ) {
39
        this.#options = options;
12,343✔
40
        this.#serialize = serialize;
12,343✔
41
        this.#values = [];
12,343✔
42
    }
43
    get( v ) {
44
        const t = typeof v;
58,947✔
45
        let code = ( ( v && ( t === `object` ) ) ? byPrototype.get( Object.getPrototypeOf( v ) ) : byType.get( t ) );
58,947✔
46
        if ( code instanceof Function ) {
58,947✔
47
            code = code( v );
25,830✔
48
        }
49
        return code && `@${ code }${ this.#values.push( v ) - 1 }@`;
58,947✔
50
    }
51
    restore( expression ) {
52
        const values = this.#values;
12,352✔
53
        if ( !values.length ) {
12,352✔
54
            return expression;
5,382✔
55
        }
56
        const options = this.#options;
6,970✔
57
        const serialize = this.#serialize;
6,970✔
58
        const serializers = {
6,970✔
59
            "A": v => `Array.prototype.slice.call(${
1✔
60
                // eslint-disable-next-line prefer-object-spread
61
                serialize( Object.assign( {
62
                    "length": v.length,
63
                }, v ), options )
64
            })`,
65
            "B": v => `BigInt("${ v }")`,
2✔
66
            "D": v => `new Date("${ v.toISOString() }")`,
2✔
67
            "F": serializeFunction,
68
            "I": identity,
69
            "L": v => `new URL("${ v.toString() }")`,
1✔
70
            "M": v => `new Map(${ serialize( Array.from( v.entries() ), options ) })`,
1✔
71
            "N": identity,
72
            "R": v => `new RegExp(${ serialize( v.source ) }, "${ v.flags }")`,
6✔
73
            "S": v => `new Set(${ serialize( Array.from( v.values() ), options ) })`,
1✔
74
            "T": v => JSON.stringify( v ),
12✔
75
            "U": identity,
76
        };
77
        return this.#values.length ? expression.replace( rPlaceholder, ( match, backSlash, type, index ) => (
6,970!
78
            // the placeholder may not be preceded by a backslash. This is to prevent
79
            // replacing things like `"a\"@R0@"` and thus outputting
80
            // invalid JS.
81
            backSlash ? match : serializers[ type ]( values[ index ] )
9,487✔
82
        ) ) : expression;
83
    }
84
}
85

86
module.exports = Placeholders;
28✔
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