GithubHelp home page GithubHelp logo

hancode's Introduction

把你的JS代码转换成中文版。。。

就是这么变态。

使用AST替换变量名,使用百度翻译服务翻译代码。

使用方式:

sudo npm install hancode -g;

#定位到某个目录

hancode -s ./code.js

#会在当前目录生成一个 code.汉.js

#code.汉.js 的代码其实就是本库的中文版,测试可以跑通,并且结果一样。

todolist

  • 翻译属性名
  • 多个文件联合编译

示例:

var 丑化JS = require("uglify-js");

var _ = require("underscore");

var 翻译工具 = require("./translate_util");

var 翻译对象 = {};

module.exports = function(源代码, 回调) {
    var AST的代码 = 丑化JS.parse(源代码);
    var 无功节点 = [];
    var 参考节点 = [];
    var 跨语言 = [];
    AST的代码.walk(new 丑化JS.TreeWalker(function() {
        if ( instanceof 丑化JS.AST_SymbolVar) {
            无功节点.push();
            跨语言.push(.name);
        }
    }));
    AST的代码.walk(new 丑化JS.TreeWalker(function() {
        if ( instanceof 丑化JS.AST_SymbolRef) {
            if (跨语言.indexOf(.name) != -1) {
                参考节点.push();
                跨语言.push(.name);
            }
        }
    }));
    跨语言 = _.uniq(跨语言);
    console.log("翻译中");
    翻译工具(跨语言, function(E, 结果) {
        翻译对象 = 结果;
        使uniq对象(翻译对象);
        无功节点.forEach(function() {
            .name = 翻译对象[.name] || .name;
        });
        参考节点.forEach(function() {
            .name = 翻译对象[.name] || .name;
        });
        回调(null, AST的代码.print_to_string({
            beautify: true
        }));
    });
};

var 使uniq对象 = function(obj) {
    var 扫描值 = [];
    for (var  in obj) {
        var 价值 = obj[];
        if (扫描值.indexOf(价值) != -1) {
            var 重复计数 = 0;
            扫描值.forEach(function(v) {
                if (v == 价值) {
                    重复计数++;
                }
            });
            obj[] = 价值 + "_" + 重复计数;
        }
        扫描值.push(价值);
    }
};

hancode's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.