Location>code7788 >text

JS script batch processing of TS data types

Popularity:265 ℃/2024-08-26 15:27:45

In TS development, often encounter more background data fields, this time you need to copy the fields one by one and then manually configure the data type to him to complete our TS type definition, quite troublesome. What is a fast way to do it , I currently encountered two cases were written JS script to deal with background data , directly generate the data format we need .

Scripting

1. Processing of data in the data dictionary

The data in a general data dictionary table may be in an excel file or in an online web page, but it is generally in the form of a table, such as the one below:
image

We only need to copy the first two columns, the fields and the field types.
After understanding our needs, start writing the js script:

// Define a method
const dealDictionaryKey = (string) => {
  // The copied data is in the form of multiple rows, so we separate each row with '\n'
  const strArr = ('\n'); { // The copied data is in multiple rows, so we separate each row with '\n'.
  let newString = ''; // Iterate over each row.
  // Iterate through each row of data
  for (let item of strArr) {
    // Skip over blank lines
    if (() === '') continue; // replace common backend data.
    // Replace common backend datatypes with js types, add /t as a separator between cell fields and cell types if you missed it
    item = (/\t/, ': ').replace('INTEGER', 'number').replace(/TIMESTAMP|DATE/, 'Date')
        .replace(/TEXT|VARCHAR\(\d*\)|CHAR\(\d*\)/, 'string');
    // Recursive because there may be more than one underscore in a field
    item = toUppercase(item);
    newString += item + ';\n';
  }
  (newString)
}
// Recursive string, replacing every underscore + lowercase letter in the string with an uppercase one
const toUppercase = (str) => {
    const idx = ('_'); // Replace until no underscores are present.
    // Replace until there are no underscores
    if(idx === -1) return str; { const idx = ('_'); // replace until no underscore.
    // underscore + lowercase letters
    const oldStr = (idx, 2); { const idx = ('_'); // return str; // underscore + lowercase letters
    // Generate uppercase letters
    const initial = (idx + 1, 1).toUpperCase(); // replace with uppercase.
    // Replace
    str = (oldStr, initial);
    str = toUppercase(str);
    return str; }
}
// Assignment test
const str =
str = toUppercase(str); return str; } // Assignment test
department_id INTEGER
department_name VARCHAR(125)
department_type CHAR(1)


create_user VARCHAR(25)
create_time TIMESTAMP
update_user VARCHAR(20)
update_time TIMESTAMP

update_time TIMESTAMP
// Execute
dealDictionaryKey(str);

Implementation results

departmentId: number;
departmentName: string;
departmentType: string;
createUser: string;
createTime: Date;
updateUser: string;
updateTime: Date;

2. Handling of replicated data in back-end code

We directly view the backend code and copy it out for processing. The general form of the backend code is as follows:
image

We copy the contents of the curly braces directly and process it. js script is written as follows:

// Define a method
const dealServerKey = (str) => {
  // The copied data is in the form of multiple rows,So let's take'\n'De-separate each row of data
  const strArr = ('\n')
  let newString = ''
  // Iterate through each row of data
  for (let item of strArr) {
    // skip over blank lines
    if (() === '' || ('private') === -1) continue;
    // Delete uselessprivatefield
    item = ('private', '').replace(';', '').trim();
    // 先将field和类型分开
    const keys = (' ');
    const type = keys[0].replace(/Integer|BigDecimal/, 'number').replace('String', 'string')
    // Replacement after processing
    newString += keys[1] + ': ' + type + ';\n';
  }
  (newString)
}
// input test
const str = `
    private Integer departmentId;

    private String departmentName;

    private String departmentType;

    private String departmentTypeName;

    private Date createTime;
`
// fulfillment
dealServerKey(str);

Implementation results

departmentId: number;
departmentName: string;
departmentType: string;
departmentTypeName: string;
createTime: Date;

utilization

After the script is written, of course, can be used directly in the browser console, each time the content of the input and call execution needs to be replaced manually. However, this is not very intuitive and convenient to use, my side will usually use theOnline execution js toolBaidu Google search a lot.
The following is based on my use of therun-js As an example:
image

Copy the script into the code box on the left and then go to replace it one at a timestr assignment, click on the center of thefulfillment button, the results can be displayed on the right side.
Finally, just copy the right side into the code.