tdewolff / minify

Go minifiers for web formats

Home Page:https://go.tacodewolff.nl/minify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how Javascipt shortening variable names works?

alixTal opened this issue · comments

I tested minifying this file, all the variable names are the same, nothing changed.

test.js

import euclideanDistance from '../../math/euclidean-distance/euclideanDistance';

export default function kNN(
  dataSet,
  labels,
  toClassify,
  k = 3,
) {
  if (!dataSet || !labels || !toClassify) {
    throw new Error('Either dataSet or labels or toClassify were not set');
  }

  // Calculate distance from toClassify to each point for all dimensions in dataSet.
  // Store distance and point's label into distances list.
  const distances = [];
  for (let i = 0; i < dataSet.length; i += 1) {
    distances.push({
      dist: euclideanDistance([dataSet[i]], [toClassify]),
      label: labels[i],
    });
  }

  // Sort distances list (from closer point to further ones).
  // Take initial k values, count with class index
  const kNearest = distances.sort((a, b) => {
    if (a.dist === b.dist) {
      return 0;
    }
    return a.dist < b.dist ? -1 : 1;
  }).slice(0, k);

  // Count the number of instances of each class in top k members.
  const labelsCounter = {};
  let topClass = 0;
  let topClassCount = 0;
  for (let i = 0; i < kNearest.length; i += 1) {
    if (kNearest[i].label in labelsCounter) {
      labelsCounter[kNearest[i].label] += 1;
    } else {
      labelsCounter[kNearest[i].label] = 1;
    }
    if (labelsCounter[kNearest[i].label] > topClassCount) {
      topClassCount = labelsCounter[kNearest[i].label];
      topClass = kNearest[i].label;
    }
  }

  // Return the class with highest count.
  return topClass;
}

output:

import euclideanDistance from"../../math/euclidean-distance/euclideanDistance";export default function(dataSet,labels,toClassify,k=3){if(!dataSet||!labels||!toClassify)throw new Error("Either dataSet or labels or toClassify were not set");const distances=[];for(let i=0;i<dataSet.length;i+=1)distances.push({dist:euclideanDistance([dataSet[i]],[toClassify]),label:labels[i]});const kNearest=distances.sort((a,b)=>a.dist===b.dist?0:a.dist<b.dist?-1:1).slice(0,k),labelsCounter={};let topClass=0,topClassCount=0;for(let i=0;i<kNearest.length;i+=1)kNearest[i].label in labelsCounter?labelsCounter[kNearest[i].label]+=1:labelsCounter[kNearest[i].label]=1,labelsCounter[kNearest[i].label]>topClassCount&&(topClassCount=labelsCounter[kNearest[i].label],topClass=kNearest[i].label);return topClass}
func main() {
  m := minify.New()
  m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
  in, _ := os.ReadFile("test.js")
  out := make([]byte, 0, len(in))
  r := buffer.NewReader(parse.Copy(in))
  w := buffer.NewWriter(out[:0])
  if err := m.Minify("application/javascript", w, r); err != nil {
    panic(err)
  }
}

I'm trying to make my javascript code a bit less readable by changing variable names to random short strings, I want to know if there is any option to do this.

I'm getting the following output when using minify:

import euclideanDistance from"../../math/euclidean-distance/euclideanDistance";export default function(e,t,n,s=3){if(!e||!t||!n)throw new Error("Either dataSet or labels or toClassify were not set");const a=[];for(let s=0;s<e.length;s+=1)a.push({dist:euclideanDistance([e[s]],[n]),label:t[s]});const o=a.sort((e,t)=>e.dist===t.dist?0:e.dist<t.dist?-1:1).slice(0,s),i={};let r=0,c=0;for(let e=0;e<o.length;e+=1)o[e].label in i?i[o[e].label]+=1:i[o[e].label]=1,i[o[e].label]>c&&(c=i[o[e].label],r=o[e].label);return r}

Not sure what's going wrong in your case...did you use KeepVariableNames?

EDIT: do you import github.com/tdewolff/minify/v2? Note the v2.

Thanks for the reply.
It works now! I think KeepVariableNames was true. (I was trying different options I forgot that it was true)
one more question, can I force to rename global variables and function names?

You can't because you wouldn't be able to use the JS from the outside! You can however wrap everything in a function like: (function(){ ... })()