Socket
Socket
Sign inDemoInstall

de-dupe

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

de-dupe - npm Package Compare versions

Comparing version 0.0.12 to 0.0.14

18

cli.js

@@ -10,3 +10,11 @@ 'use strict';

var USE_STRICT = 'use strict';
var RESERVED_WORDS = "\n abstract arguments await boolean break byte case catch char class const continue debugger\n default delete do double else enum eval export extends false final finally float for\n function goto if implements import in instanceof int interface let long native new null\n package private protected public return short static super switch synchronized this throw\n throws transient true try typeof var void volatile while with yield".trim().split(/[\s]+/g);
var RESERVED_WORDS = ['abstract', 'arguments', 'await', 'boolean', 'break', 'byte', 'case',
'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default',
'delete', 'do', 'double', 'else', 'enum', 'eval', 'export', 'extends',
'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if',
'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let',
'long', 'native', 'new', 'null', 'package', 'private', 'protected',
'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized',
'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var',
'void', 'volatile', 'while', 'with', 'yield'];
var Dedupe = (function () {

@@ -17,3 +25,2 @@ function Dedupe(options) {

addScope: false,
cleanStrings: false,
includeReplacements: false,

@@ -267,3 +274,3 @@ minInstances: -1,

}
codeBuffer.push(this.cleanString(replacement.text));
codeBuffer.push(replacement.text);
if (BOUNDARY.test(code.charAt(replacement.end))) {

@@ -300,7 +307,2 @@ codeBuffer.push(' ');

};
Dedupe.prototype.cleanString = function (fatString) {
return this.options.cleanStrings
? fatString.replace(/[\s]+/g, ' ')
: fatString;
};
Dedupe.prototype.getTopLevelScopes = function (node) {

@@ -307,0 +309,0 @@ if (typeof node === 'undefined') {

export interface DedupeOptions {
/**
* Specify the deduplication algorithm.
* gzip - Apply an algorithm that works best for files that will be gzipped
* all -
* Specify a de-duplication method
* gzip - Works best for files that will be gzipped
* all - Generic method to de-duplicate all strings
*/

@@ -14,7 +14,2 @@ type?: 'gzip' | 'all';

/**
* Removes duplicate spaces from strings, usually strings in javascript render
* to the DOM and more than one space in the DOM is ignored and just bloats scripts
*/
cleanStrings?: boolean;
/**
* Should the result include the replacements

@@ -24,7 +19,7 @@ */

/**
* Minimum number of string instances before de-dupe will replace the string.
* Minimum number of string instances before de-dupe will replace the string. (Only works when type === 'all')
*/
minInstances?: number;
/**
* Minimum length of the string before de-dupe will replace the string.
* Minimum length of the string before de-dupe will replace the string. (Only works when type === 'all'
*/

@@ -57,3 +52,2 @@ minLength?: number;

private getStartingPositionOfScope(scope);
private cleanString(fatString);
private getTopLevelScopes(node?);

@@ -60,0 +54,0 @@ private getTopLevelFunctions(node?);

@@ -8,3 +8,11 @@ 'use strict';

var USE_STRICT = 'use strict';
var RESERVED_WORDS = "\n abstract arguments await boolean break byte case catch char class const continue debugger\n default delete do double else enum eval export extends false final finally float for\n function goto if implements import in instanceof int interface let long native new null\n package private protected public return short static super switch synchronized this throw\n throws transient true try typeof var void volatile while with yield".trim().split(/[\s]+/g);
var RESERVED_WORDS = ['abstract', 'arguments', 'await', 'boolean', 'break', 'byte', 'case',
'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default',
'delete', 'do', 'double', 'else', 'enum', 'eval', 'export', 'extends',
'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if',
'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let',
'long', 'native', 'new', 'null', 'package', 'private', 'protected',
'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized',
'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var',
'void', 'volatile', 'while', 'with', 'yield'];
var Dedupe = (function () {

@@ -15,3 +23,2 @@ function Dedupe(options) {

addScope: false,
cleanStrings: false,
includeReplacements: false,

@@ -265,3 +272,3 @@ minInstances: -1,

}
codeBuffer.push(this.cleanString(replacement.text));
codeBuffer.push(replacement.text);
if (BOUNDARY.test(code.charAt(replacement.end))) {

@@ -298,7 +305,2 @@ codeBuffer.push(' ');

};
Dedupe.prototype.cleanString = function (fatString) {
return this.options.cleanStrings
? fatString.replace(/[\s]+/g, ' ')
: fatString;
};
Dedupe.prototype.getTopLevelScopes = function (node) {

@@ -305,0 +307,0 @@ if (typeof node === 'undefined') {

{
"name": "de-dupe",
"bin": ".bin/de-dupe",
"version": "0.0.12",
"version": "0.0.14",
"description": "Deduplicate strings from javascript assets",

@@ -33,14 +33,14 @@ "main": "index.js",

"dependencies": {
"typescript": "^2.2.1"
"typescript": "^2.3.2"
},
"devDependencies": {
"@types/chai": "^3.4.35",
"@types/mocha": "^2.2.40",
"@types/node": "^7.0.8",
"@types/chai": "^3.5.2",
"@types/mocha": "^2.2.41",
"@types/node": "^7.0.18",
"chai": "^3.5.0",
"mocha": "^3.2.0",
"rollup": "^0.41.5",
"mocha": "^3.3.0",
"rollup": "^0.41.6",
"rollup-plugin-typescript": "^0.8.1",
"tslint": "^4.4.2"
"tslint": "^5.2.0"
}
}

@@ -19,3 +19,3 @@ import { readFile, writeFile } from 'fs';

function processFiles(opt: typeof options) {
for (let file of opt.files) {
for (const file of opt.files) {
readFile(file, 'utf-8', (err, code) => {

@@ -22,0 +22,0 @@ if (err) {

@@ -15,5 +15,5 @@ import {

/**
* Specify the deduplication algorithm.
* gzip - Apply an algorithm that works best for files that will be gzipped
* all -
* Specify a de-duplication method
* gzip - Works best for files that will be gzipped
* all - Generic method to de-duplicate all strings
*/

@@ -27,7 +27,2 @@ type?: 'gzip' | 'all';

/**
* Removes duplicate spaces from strings, usually strings in javascript render
* to the DOM and more than one space in the DOM is ignored and just bloats scripts
*/
cleanStrings?: boolean;
/**
* Should the result include the replacements

@@ -37,7 +32,7 @@ */

/**
* Minimum number of string instances before de-dupe will replace the string.
* Minimum number of string instances before de-dupe will replace the string. (Only works when type === 'all')
*/
minInstances?: number;
/**
* Minimum length of the string before de-dupe will replace the string.
* Minimum length of the string before de-dupe will replace the string. (Only works when type === 'all'
*/

@@ -47,7 +42,5 @@ minLength?: number;

interface StringMap extends Map<string, StringLiteral[]> { }
type StringMap = Map<string, StringLiteral[]>;
interface StatementAction {
(statement: Node): void;
}
type StatementAction = (statement: Node) => void;

@@ -68,8 +61,11 @@ export interface StringReplacement {

const USE_STRICT = 'use strict';
const RESERVED_WORDS = `
abstract arguments await boolean break byte case catch char class const continue debugger
default delete do double else enum eval export extends false final finally float for
function goto if implements import in instanceof int interface let long native new null
package private protected public return short static super switch synchronized this throw
throws transient true try typeof var void volatile while with yield`.trim().split(/[\s]+/g);
const RESERVED_WORDS = [ 'abstract', 'arguments', 'await', 'boolean', 'break', 'byte', 'case',
'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default',
'delete', 'do', 'double', 'else', 'enum', 'eval', 'export', 'extends',
'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if',
'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let',
'long', 'native', 'new', 'null', 'package', 'private', 'protected',
'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized',
'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var',
'void', 'volatile', 'while', 'with', 'yield'];

@@ -81,3 +77,2 @@ export default class Dedupe {

addScope: false,
cleanStrings: false,
includeReplacements: false,

@@ -103,7 +98,7 @@ minInstances: -1,

const topLevelScopes = this.getTopLevelScopes(sourceFile);
const identifiers = <Map<string, string>> (<any> sourceFile).identifiers;
const identifiers = (sourceFile as any).identifiers as Map<string, string>;
const usedIdentifiers = new Map<string, string>(identifiers);
const reservedWords = RESERVED_WORDS;
for (let word of reservedWords) {
for (const word of reservedWords) {
usedIdentifiers.set(word, word);

@@ -168,3 +163,3 @@ }

for (let key of stringKeysToReplace) {
for (const key of stringKeysToReplace) {
const values = stringMap.get(key);

@@ -178,5 +173,5 @@ if (!values) {

for (let j = 0, length = values.length; j < length; j++) {
let node = values[j];
let start = node.getStart();
let end = node.getEnd();
const node = values[j];
const start = node.getStart();
const end = node.getEnd();

@@ -207,3 +202,3 @@ replacements.push({

const replacements: StringReplacement[] = [];
let rankingMap: Array<Array<string | number>> = [];
const rankingMap: Array<Array<string | number>> = [];

@@ -238,5 +233,5 @@ const minLength = typeof this.options.minLength === 'undefined' ? -1 : this.options.minLength;

for (let ranking of rankingMap) {
let key = ranking[0] as string;
let values = stringMap.get(key);
for (const ranking of rankingMap) {
const key = ranking[0] as string;
const values = stringMap.get(key);
if (values) {

@@ -247,5 +242,5 @@ const variableName = this.getUniqueVariableName(usedVariableNames);

for (let j = 0, length = values.length; j < length; j++) {
let node = values[j];
let start = node.getStart();
let end = node.getEnd();
const node = values[j];
const start = node.getStart();
const end = node.getEnd();

@@ -361,3 +356,3 @@ replacements.push({

for (let i = 0, length = sortedReplacements.length; i < length; i++) {
let replacement = sortedReplacements[i];
const replacement = sortedReplacements[i];

@@ -368,3 +363,3 @@ codeBuffer.push(code.substring(cursor, replacement.start));

}
codeBuffer.push(this.cleanString(replacement.text));
codeBuffer.push(replacement.text);
if (BOUNDARY.test(code.charAt(replacement.end))) {

@@ -407,8 +402,2 @@ codeBuffer.push(' ');

private cleanString(fatString: string): string {
return this.options.cleanStrings
? fatString.replace(/[\s]+/g, ' ')
: fatString;
}
private getTopLevelScopes(node?: Node) {

@@ -421,3 +410,3 @@ if (typeof node === 'undefined') {

const functions = this.getTopLevelFunctions(node);
for (let functionNode of functions) {
for (const functionNode of functions) {
const blocks = this.getTopLevelFunctionBlocks(functionNode);

@@ -434,6 +423,6 @@ scopes = scopes.concat(blocks);

if (
node.kind !== SyntaxKind.ArrowFunction &&
node.kind !== SyntaxKind.FunctionDeclaration &&
node.kind !== SyntaxKind.FunctionExpression &&
node.kind !== SyntaxKind.FunctionType
node.kind !== SyntaxKind.ArrowFunction &&
node.kind !== SyntaxKind.FunctionDeclaration &&
node.kind !== SyntaxKind.FunctionExpression &&
node.kind !== SyntaxKind.FunctionType
) {

@@ -440,0 +429,0 @@ forEachChild(node, (childNode: Node) => {

@@ -10,3 +10,11 @@ 'use strict';

var USE_STRICT = 'use strict';
var RESERVED_WORDS = "\n abstract arguments await boolean break byte case catch char class const continue debugger\n default delete do double else enum eval export extends false final finally float for\n function goto if implements import in instanceof int interface let long native new null\n package private protected public return short static super switch synchronized this throw\n throws transient true try typeof var void volatile while with yield".trim().split(/[\s]+/g);
var RESERVED_WORDS = ['abstract', 'arguments', 'await', 'boolean', 'break', 'byte', 'case',
'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default',
'delete', 'do', 'double', 'else', 'enum', 'eval', 'export', 'extends',
'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if',
'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let',
'long', 'native', 'new', 'null', 'package', 'private', 'protected',
'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized',
'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var',
'void', 'volatile', 'while', 'with', 'yield'];
var Dedupe = (function () {

@@ -17,3 +25,2 @@ function Dedupe(options) {

addScope: false,
cleanStrings: false,
includeReplacements: false,

@@ -267,3 +274,3 @@ minInstances: -1,

}
codeBuffer.push(this.cleanString(replacement.text));
codeBuffer.push(replacement.text);
if (BOUNDARY.test(code.charAt(replacement.end))) {

@@ -300,7 +307,2 @@ codeBuffer.push(' ');

};
Dedupe.prototype.cleanString = function (fatString) {
return this.options.cleanStrings
? fatString.replace(/[\s]+/g, ' ')
: fatString;
};
Dedupe.prototype.getTopLevelScopes = function (node) {

@@ -357,18 +359,5 @@ if (typeof node === 'undefined') {

var dedupe = new Dedupe({
type: 'gzip',
minInstances: 2,
minLength: 2
});
var stringCleaner = new Dedupe({
type: 'all',
cleanStrings: true,
minInstances: 2,
minLength: 2
});
var dedupe = new Dedupe();
var scopeAdder = new Dedupe({
addScope: true,
type: 'all',
minInstances: 2,
minLength: 2
addScope: true
});

@@ -432,7 +421,2 @@ var gzipSpace = Array(32800).join(' ');

});
it('will clean strings', function () {
var code = "\n !function() {\n console.log('z ', " + gzipSpace + " 'z ', 'z ', 'z ', 'z ', 'z ', 'z ', 'z ');\n }()\n ";
var result = stringCleaner.dedupe(code);
chai.expect(result.code).contain("\"z \"");
});
it('will not effect non-function blocks', function () {

@@ -439,0 +423,0 @@ var code = "\n if (true) {\n console.log('z', " + gzipSpace + " 'z', 'z', 'z', 'z', 'z');\n }\n ";

@@ -6,18 +6,5 @@ import 'mocha';

const dedupe = new Dedupe({
type: 'gzip',
minInstances: 2,
minLength: 2
});
const stringCleaner = new Dedupe({
type: 'all',
cleanStrings: true,
minInstances: 2,
minLength: 2
});
const dedupe = new Dedupe();
const scopeAdder = new Dedupe({
addScope: true,
type: 'all',
minInstances: 2,
minLength: 2
addScope: true
});

@@ -126,14 +113,2 @@

it('will clean strings', () => {
const code = `
!function() {
console.log('z ', ${gzipSpace} 'z ', 'z ', 'z ', 'z ', 'z ', 'z ', 'z ');
}()
`;
const result = stringCleaner.dedupe(code);
expect(result.code).contain(`"z "`);
});
it('will not effect non-function blocks', () => {

@@ -216,3 +191,2 @@ const code = `

});
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