Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magic-string

Package Overview
Dependencies
Maintainers
1
Versions
116
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-string - npm Package Compare versions

Comparing version 0.2.4 to 0.2.5

423

magic-string.js

@@ -20,8 +20,8 @@ (function (global, factory) {

var btoa___btoa;
var _btoa;
if ( typeof window !== 'undefined' && typeof window.btoa === 'function' ) {
btoa___btoa = window.btoa;
_btoa = window.btoa;
} else if ( typeof Buffer === 'function' ) {
btoa___btoa = function ( str ) {
_btoa = function ( str ) {
return new Buffer( str ).toString( 'base64' );

@@ -32,8 +32,8 @@ };

}
var btoa__default = btoa___btoa;
var SourceMap__SourceMap = function ( properties ) {
var btoa = _btoa;
var SourceMap = function ( properties ) {
this.version = 3;
this.file = properties.file;

@@ -45,23 +45,23 @@ this.sources = properties.sources;

};
SourceMap__SourceMap.prototype = {
SourceMap.prototype = {
toString: function () {
return JSON.stringify( this );
},
toUrl: function () {
return 'data:application/json;charset=utf-8;base64,' + btoa__default( this.toString() );
return 'data:application/json;charset=utf-8;base64,' + btoa( this.toString() );
}
};
var SourceMap__default = SourceMap__SourceMap;
function getRelativePath__getRelativePath ( from, to ) {
var SourceMap__default = SourceMap;
function getRelativePath ( from, to ) {
var fromParts, toParts, i;
fromParts = from.split( '/' );
toParts = to.split( '/' );
fromParts.pop(); // get dirname
while ( fromParts[0] === toParts[0] ) {

@@ -71,3 +71,3 @@ fromParts.shift();

}
if ( fromParts.length ) {

@@ -77,3 +77,3 @@ i = fromParts.length;

}
return fromParts.concat( toParts ).join( '/' );

@@ -85,15 +85,14 @@ // } else {

}
var getRelativePath__default = getRelativePath__getRelativePath;
var Bundle__Bundle = function ( options ) {
var Bundle = function ( options ) {
options = options || {};
this.intro = options.intro || '';
this.outro = options.outro || '';
this.separator = 'separator' in options ? options.separator : '\n';
this.sources = [];
};
Bundle__Bundle.prototype = {
Bundle.prototype = {
addSource: function ( source ) {

@@ -103,7 +102,7 @@ if ( typeof source !== 'object' || !source.content ) {

}
this.sources.push( source );
return this;
},
append: function ( str ) {

@@ -113,5 +112,5 @@ this.outro += str;

},
clone: function () {
var bundle = new Bundle__Bundle({
var bundle = new Bundle({
intro: this.intro,

@@ -121,3 +120,3 @@ outro: this.outro,

});
this.sources.forEach( function ( source ) {

@@ -129,23 +128,23 @@ bundle.addSource({

});
return bundle;
},
generateMap: function ( options ) {
var offsets = {}, encoded, encodingSeparator;
encodingSeparator = Bundle__getSemis( this.separator );
encodingSeparator = getSemis( this.separator );
encoded = (
Bundle__getSemis( this.intro ) +
getSemis( this.intro ) +
this.sources.map( function ( source, sourceIndex) {
return source.content.getMappings( options.hires, sourceIndex, offsets );
}).join( encodingSeparator ) +
Bundle__getSemis( this.outro )
getSemis( this.outro )
);
return new SourceMap__default({
return new SourceMap({
file: options.file.split( '/' ).pop(),
sources: this.sources.map( function ( source ) {
return getRelativePath__default( options.file, source.filename );
return getRelativePath( options.file, source.filename );
}),

@@ -159,13 +158,13 @@ sourcesContent: this.sources.map( function ( source ) {

},
getIndentString: function () {
var indentStringCounts = {};
this.sources.forEach( function ( source ) {
var indentStr = source.content.indentStr;
if ( !indentStringCounts[ indentStr ] ) indentStringCounts[ indentStr ] = 0;
indentStringCounts[ indentStr ] += 1;
});
return ( Object.keys( indentStringCounts ).sort( function ( a, b ) {

@@ -175,3 +174,3 @@ return indentStringCounts[a] - indentStringCounts[b];

},
indent: function ( indentStr ) {

@@ -181,13 +180,13 @@ if ( !indentStr ) {

}
this.sources.forEach( function ( source ) {
source.content.indent( indentStr );
source.content.indent( indentStr, { exclude: source.indentExclusionRanges });
});
this.intro = ( this.intro ? indentStr : '' ) + this.intro.replace( /\n(.+)/g, ( '\n' + indentStr + '$1' ) );
this.outro = this.outro.replace( /\n(.+)/g, ( '\n' + indentStr + '$1' ) );
return this;
},
prepend: function ( str ) {

@@ -197,13 +196,13 @@ this.intro = str + this.intro;

},
toString: function () {
return this.intro + this.sources.map( Bundle__stringify ).join( this.separator ) + this.outro;
return this.intro + this.sources.map( stringify ).join( this.separator ) + this.outro;
},
trim: function () {
var i, source;
this.intro = this.intro.replace( /^\s+/, '' );
this.outro = this.outro.replace( /\s+$/, '' );
// trim start

@@ -214,3 +213,3 @@ if ( !this.intro ) {

source = this.sources[i];
if ( !source ) {

@@ -220,3 +219,3 @@ this.outro = this.outro.replace( /^\s+/, '' );

}
source.content.trimStart();

@@ -226,3 +225,3 @@ i += 1;

}
// trim end

@@ -233,3 +232,3 @@ if ( !this.outro ) {

source = this.sources[i];
if ( !source ) {

@@ -239,3 +238,3 @@ this.intro = this.intro.replace( /\s+$/, '' );

}
source.content.trimEnd();

@@ -245,30 +244,30 @@ i -= 1;

}
return this;
}
};
var Bundle__default = Bundle__Bundle;
function Bundle__stringify ( source ) {
var Bundle__default = Bundle;
function stringify ( source ) {
return source.content.toString();
}
function Bundle__getSemis ( str ) {
function getSemis ( str ) {
return new Array( str.split( '\n' ).length ).join( ';' );
}
function guessIndent__guessIndent ( code ) {
function guessIndent ( code ) {
var lines, tabbed, spaced, min;
lines = code.split( '\n' );
tabbed = lines.filter( function ( line ) {
return /^\t+/.test( line );
});
spaced = lines.filter( function ( line ) {
return /^ +/.test( line );
return /^ {2,}/.test( line );
});
// More lines tabbed than spaced? Assume tabs, and

@@ -280,3 +279,3 @@ // default to tabs in the case of a tie (or nothing

}
// Otherwise, we need to guess the multiple

@@ -287,8 +286,7 @@ min = spaced.reduce( function ( previous, current ) {

}, Infinity );
return new Array( min + 1 ).join( ' ' );
}
var guessIndent__default = guessIndent__guessIndent;
function encodeMappings__encodeMappings ( original, str, mappings, hires, sourceIndex, offsets ) {
function encodeMappings ( original, str, mappings, hires, sourceIndex, offsets ) {
var lineStart,

@@ -301,3 +299,3 @@ locations,

firstSegment = true;
// store locations, for fast lookup

@@ -308,13 +306,13 @@ lineStart = 0;

lineStart += line.length + 1; // +1 for the newline
return start;
});
inverseMappings = encodeMappings__invert( str, mappings );
inverseMappings = invert( str, mappings );
lines = str.split( '\n' ).map( function ( line ) {
var segments, len, char, origin, lastOrigin, i, location;
segments = [];
len = line.length;

@@ -324,3 +322,3 @@ for ( i = 0; i < len; i += 1 ) {

origin = inverseMappings[ char ];
if ( !~origin ) {

@@ -338,3 +336,3 @@ if ( !~lastOrigin ) {

}
else {

@@ -344,4 +342,4 @@ if ( !hires && ( origin === lastOrigin + 1 ) ) {

} else {
location = encodeMappings__getLocation( locations, origin );
location = getLocation( locations, origin );
segments.push({

@@ -355,19 +353,19 @@ generatedCodeColumn: i,

}
lastOrigin = origin;
}
charOffset += line.length + 1;
return segments;
});
offsets = offsets || {};
offsets.sourceIndex = offsets.sourceIndex || 0;
offsets.sourceCodeLine = offsets.sourceCodeLine || 0;
offsets.sourceCodeColumn = offsets.sourceCodeColumn || 0;
encoded = lines.map( function ( segments ) {
var generatedCodeColumn = 0;
return segments.map( function ( segment ) {

@@ -380,3 +378,3 @@ var arr = [

];
generatedCodeColumn = segment.generatedCodeColumn;

@@ -386,17 +384,16 @@ offsets.sourceIndex = segment.sourceIndex;

offsets.sourceCodeColumn = segment.sourceCodeColumn;
firstSegment = false;
return vlq__default.encode( arr );
}).join( ',' );
}).join( ';' );
return encoded;
}
var encodeMappings__default = encodeMappings__encodeMappings;
function encodeMappings__invert ( str, mappings ) {
function invert ( str, mappings ) {
var inverted = new Uint32Array( str.length ), i;
// initialise everything to -1

@@ -407,3 +404,3 @@ i = str.length;

}
// then apply the actual mappings

@@ -416,9 +413,9 @@ i = mappings.length;

}
return inverted;
}
function encodeMappings__getLocation ( locations, char ) {
function getLocation ( locations, char ) {
var i;
i = locations.length;

@@ -433,14 +430,14 @@ while ( i-- ) {

}
throw new Error( 'Character out of bounds' );
}
var index__MagicString = function ( string ) {
var MagicString = function ( string ) {
this.original = this.str = string;
this.mappings = index__initMappings( string.length );
this.indentStr = guessIndent__default( string );
this.mappings = initMappings( string.length );
this.indentStr = guessIndent( string );
};
index__MagicString.prototype = {
MagicString.prototype = {
append: function ( content ) {

@@ -450,9 +447,9 @@ this.str += content;

},
clone: function () {
var clone, i;
clone = new index__MagicString( this.original );
clone = new MagicString( this.original );
clone.str = this.str;
i = clone.mappings.length;

@@ -462,12 +459,12 @@ while ( i-- ) {

}
return clone;
},
generateMap: function ( options ) {
options = options || {};
return new SourceMap__default({
return new SourceMap({
file: ( options.file ? options.file.split( '/' ).pop() : null ),
sources: [ options.source ? getRelativePath__default( options.file || '', options.source ) : null ],
sources: [ options.source ? getRelativePath( options.file || '', options.source ) : null ],
sourcesContent: options.includeContent ? [ this.original ] : [ null ],

@@ -478,11 +475,11 @@ names: [],

},
getMappings: function ( hires, sourceIndex, offsets ) {
return encodeMappings__default( this.original, this.str, this.mappings, hires, sourceIndex, offsets );
return encodeMappings( this.original, this.str, this.mappings, hires, sourceIndex, offsets );
},
indent: function ( indentStr, options ) {
var self = this,
mappings = this.mappings,
reverseMappings = index__reverse( mappings, this.str.length ),
reverseMappings = reverse( mappings, this.str.length ),
pattern = /\n(.)/g,

@@ -495,3 +492,3 @@ match,

i;
if ( typeof indentStr === 'object' ) {

@@ -501,28 +498,28 @@ options = indentStr;

}
indentStr = indentStr !== undefined ? indentStr : this.indentStr;
options = options || {};
// Process exclusion ranges
if ( options.exclude ) {
exclusions = typeof options.exclude[0] === 'number' ? [ options.exclude ] : options.exclude;
exclusions = exclusions.map( function ( range ) {
var rangeStart, rangeEnd;
rangeStart = self.locate( range[0] );
rangeEnd = self.locate( range[1] );
if ( rangeStart === null || rangeEnd === null ) {
throw new Error( 'Cannot use indices of replaced characters as exclusion ranges' );
}
return [ rangeStart, rangeEnd ];
});
exclusions.sort( function ( a, b ) {
return a[0] - b[0];
});
// check for overlaps

@@ -534,7 +531,7 @@ lastEnd = -1;

}
lastEnd = range[1];
});
}
if ( !exclusions ) {

@@ -544,3 +541,3 @@ while ( match = pattern.exec( this.str ) ) {

}
this.str = indentStr + this.str.replace( pattern, '\n' + indentStr + '$1' );

@@ -553,3 +550,3 @@ } else {

}
this.str = indentStr + this.str.replace( pattern, function ( match, $1, index ) {

@@ -559,32 +556,32 @@ return isExcluded( index ) ? match : '\n' + indentStr + $1;

}
adjustments = inserts.map( function ( index ) {
var origin;
do {
origin = reverseMappings[ index++ ];
} while ( !~origin && index < self.str.length );
return origin;
});
i = adjustments.length;
lastEnd = this.mappings.length;
while ( i-- ) {
index__adjust( self.mappings, adjustments[i], lastEnd, ( ( i + 1 ) * indentStr.length ) );
adjust( self.mappings, adjustments[i], lastEnd, ( ( i + 1 ) * indentStr.length ) );
lastEnd = adjustments[i];
}
return this;
function isExcluded ( index ) {
var i = exclusions.length, range;
while ( i-- ) {
range = exclusions[i];
if ( range[1] < index ) {
return false;
}
if ( range[0] <= index ) {

@@ -596,3 +593,3 @@ return true;

},
insert: function ( index, content ) {

@@ -606,25 +603,25 @@ if ( index === 0 ) {

}
return this;
},
// get current location of character in original string
locate: function ( character ) {
var loc;
if ( character < 0 || character > this.mappings.length ) {
throw new Error( 'Character is out of bounds' );
}
loc = this.mappings[ character ];
return ~loc ? loc : null;
},
locateOrigin: function ( character ) {
var i;
if ( character < 0 || character >= this.str.length ) {
throw new Error( 'Character is out of bounds' );
}
i = this.mappings.length;

@@ -636,12 +633,12 @@ while ( i-- ) {

}
return null;
},
prepend: function ( content ) {
this.str = content + this.str;
index__adjust( this.mappings, 0, this.mappings.length, content.length );
adjust( this.mappings, 0, this.mappings.length, content.length );
return this;
},
remove: function ( start, end ) {

@@ -651,46 +648,46 @@ this.replace( start, end, '' );

},
replace: function ( start, end, content ) {
var firstChar, lastChar, d;
firstChar = this.locate( start );
lastChar = this.locate( end - 1 );
if ( firstChar === null || lastChar === null ) {
throw new Error( 'Cannot replace the same content twice' );
}
this.str = this.str.substr( 0, firstChar ) + content + this.str.substring( lastChar + 1 );
d = content.length - ( lastChar + 1 - firstChar );
index__blank( this.mappings, start, end );
index__adjust( this.mappings, end, this.mappings.length, d );
blank( this.mappings, start, end );
adjust( this.mappings, end, this.mappings.length, d );
return this;
},
slice: function ( start, end ) {
var firstChar, lastChar;
firstChar = this.locate( start );
lastChar = this.locate( end - 1 ) + 1;
if ( firstChar === null || lastChar === null ) {
throw new Error( 'Cannot use replaced characters as slice anchors' );
}
return this.str.slice( firstChar, lastChar );
},
toString: function () {
return this.str;
},
trim: function () {
return this.trimStart().trimEnd();
},
trimEnd: function () {
var self = this;
this.str = this.str.replace( /\s+$/, function ( trailing, index, str ) {

@@ -701,3 +698,3 @@ var strLength = str.length,

chars = [];
i = strLength;

@@ -707,3 +704,3 @@ while ( i-- > strLength - length ) {

}
i = chars.length;

@@ -715,15 +712,15 @@ while ( i-- ) {

}
return '';
});
return this;
},
trimStart: function () {
var self = this;
this.str = this.str.replace( /^\s+/, function ( leading ) {
var length = leading.length, i, chars = [], adjustmentStart = 0;
i = length;

@@ -733,3 +730,3 @@ while ( i-- ) {

}
i = chars.length;

@@ -742,15 +739,15 @@ while ( i-- ) {

}
index__adjust( self.mappings, adjustmentStart, self.mappings.length, -length );
adjust( self.mappings, adjustmentStart, self.mappings.length, -length );
return '';
});
return this;
}
};
index__MagicString.Bundle = Bundle__default;
function index__adjust ( mappings, start, end, d ) {
MagicString.Bundle = Bundle;
function adjust ( mappings, start, end, d ) {
var i = end;

@@ -763,14 +760,14 @@ while ( i-- > start ) {

}
function index__initMappings ( i ) {
function initMappings ( i ) {
var mappings = new Uint32Array( i );
while ( i-- ) {
mappings[i] = i;
}
return mappings;
}
function index__blank ( mappings, start, i ) {
function blank ( mappings, start, i ) {
while ( i-- > start ) {

@@ -780,16 +777,16 @@ mappings[i] = -1;

}
function index__reverse ( mappings, i ) {
function reverse ( mappings, i ) {
var result, location;
result = new Uint32Array( i );
while ( i-- ) {
result[i] = -1;
}
i = mappings.length;
while ( i-- ) {
location = mappings[i];
if ( ~location ) {

@@ -799,11 +796,11 @@ result[ location ] = i;

}
return result;
}
var index__default = index__MagicString;
return index__default;
var index = MagicString;
return index;
}));
//# sourceMappingURL=./magic-string.js.map

@@ -5,3 +5,3 @@ {

"author": "Rich Harris",
"version": "0.2.4",
"version": "0.2.5",
"repository": "https://github.com/rich-harris/magic-string",

@@ -8,0 +8,0 @@ "main": "magic-string.js",

@@ -72,6 +72,8 @@ # magic-string

### s.indent( prefix )
### s.indent( prefix[, options] )
Prefixes each line of the string with `prefix`. If `prefix` is not supplied, the indentation will be guessed from the original content, falling back to a single tab character. Returns `this`.
The `options` argument can have an `exclude` property, which is an array of `[start, end]` character ranges. These ranges will be excluded from the indentation - useful for (e.g.) multiline strings.
### s.insert( index, content )

@@ -118,3 +120,3 @@

filename: 'foo.js',
content: new MagicString( 'var answer = 42;' ).indent()
content: new MagicString( 'var answer = 42;' )
});

@@ -124,6 +126,10 @@

filename: 'bar.js',
content: new MagicString( 'console.log( answer )' ).indent()
content: new MagicString( 'console.log( answer )' )
});
bundle.indent()
// Advanced: a source can include an `indentExclusionRanges` property
// alongside `filename` and `content`. This will be passed to `s.indent()`
// - see documentation above
bundle.indent() // optionally, pass an indent string, otherwise it will be guessed
.prepend( '(function () {\n' )

@@ -144,2 +150,3 @@ .append( '}());' );

});
```

@@ -146,0 +153,0 @@ ## License

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc