zoukankan      html  css  js  c++  java
  • [Tools] VS Code Tips

    Inside one file, you can freely mark the number 1-9:

    shift + cmd + [1-9]

    And jump to Number of bookmark:

    cmd + [1-9]


    It helps to generate type safe interface based the json file you provided.

    For example you can create a json file called pokemon.json:

    {
        "id": 1,
        "name": "Bulbasaur",
        "img": "http://www.serebii.net/pokemongo/pokemon/001.png",
        "type": [ "Grass", "Poison" ],
        "weaknesses": [ "Fire", "Ice", "Flying", "Psychic" ]
    }

    Then in the file you want to generate the code, open vs cammand panel, enter "Paste Json...", enter "Pokemon"...

    It will genearte type safe code and lots of utitlties code.

    export interface Pokemon {
      id:         number;
      name:       string;
      img:        string;
      type:       string[];
      weaknesses: string[];
    }
    
    // Converts JSON strings to/from your types
    // and asserts the results of JSON.parse at runtime
    export namespace Convert {
      export function toPokemon(json: string): Pokemon {
        return cast(JSON.parse(json), r("Pokemon"));
      }
    
      export function pokemonToJson(value: Pokemon): string {
        return JSON.stringify(value, null, 2);
      }
    
      function cast<T>(obj: any, typ: any): T {
        if (!isValid(typ, obj)) {
          throw Error(`Invalid value`);
        }
        return obj;
      }
    
      function isValid(typ: any, val: any): boolean {
        if (typ === "any") { return true; }
        if (typ === null) { return val === null; }
        if (typ === false) { return false; }
        while (typeof typ === "object" && typ.ref !== undefined) {
          typ = typeMap[typ.ref];
        }
        if (Array.isArray(typ)) { return isValidEnum(typ, val); }
        if (typeof typ === "object") {
          return typ.hasOwnProperty("unionMembers") ? isValidUnion(typ.unionMembers, val)
            : typ.hasOwnProperty("arrayItems")    ? isValidArray(typ.arrayItems, val)
            : typ.hasOwnProperty("props")         ? isValidObject(typ.props, typ.additional, val)
            : false;
        }
        return isValidPrimitive(typ, val);
      }
    
      function isValidPrimitive(typ: string, val: any) {
        return typeof typ === typeof val;
      }
    
      function isValidUnion(typs: any[], val: any): boolean {
        // val must validate against one typ in typs
        return typs.some((typ) => isValid(typ, val));
      }
    
      function isValidEnum(cases: string[], val: any): boolean {
        return cases.indexOf(val) !== -1;
      }
    
      function isValidArray(typ: any, val: any): boolean {
        // val must be an array with no invalid elements
        return Array.isArray(val) && val.every((element) => {
          return isValid(typ, element);
        });
      }
    
      function isValidObject(props: { [k: string]: any }, additional: any, val: any): boolean {
        if (val === null || typeof val !== "object" || Array.isArray(val)) {
          return false;
        }
        return Object.getOwnPropertyNames(val).every((key) => {
          const prop = val[key];
          if (Object.prototype.hasOwnProperty.call(props, key)) {
            return isValid(props[key], prop);
          }
          return isValid(additional, prop);
        });
      }
    
      function a(typ: any) {
        return { arrayItems: typ };
      }
    
      function u(...typs: any[]) {
        return { unionMembers: typs };
      }
    
      function o(props: { [k: string]: any }, additional: any) {
        return { props, additional };
      }
    
      function m(additional: any) {
        return { props: {}, additional };
      }
    
      function r(name: string) {
        return { ref: name };
      }
    
      const typeMap: any = {
        "Pokemon": o({
          id: 0,
          name: "",
          img: "",
          type: a(""),
          weaknesses: a(""),
        }, false),
      };
    }
    

      

    A easy way to dealing with Git.

    Good for demoing the code in a team.

  • 相关阅读:
    需求工程阅读笔记03
    需求工程阅读笔记02
    【Augmented Reality】增强现实中的光学透射式头盔显示器的标定进阶
    基于单个RGB摄像头的手势识别程序设计与实现
    将Vuforia程序发布到Windows10系统的基本流程
    基于Unity3D 的Vuforia SDK开发基础教程
    微软KinectV2深度传感器在Ubuntu上的配置和使用
    Windows 10(64位)配置Caffe运行环境的基本流程
    Ubuntu14.04 64位配置Caffe 教程(基于CUDA7.5)
    空间增强现实——基于贝塞尔曲面的异形表面投影变形技术
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9270153.html
Copyright © 2011-2022 走看看