EdwardZZZ / articles

工作点滴记录
2 stars 0 forks source link

format #48

Open EdwardZZZ opened 6 years ago

EdwardZZZ commented 6 years ago
const str = 'Map<Map<String, Map<String, String>>, List<Map<Integer, Integer>>>'.replace(/\s/g, '');
console.log(str);

function formatT(str) {
    let newStr = str.replace(/(^\s*)|(\s*$)/g, "");
    const result = {};

    const regArr = newStr.match(/^([^<>]+)<(.+)>$/);
    if (!(regArr && regArr.length === 3)) {
        result.$class = str;
        return result;
    }

    const type = regArr[1];
    newStr = regArr[2];

    result.$class = type;
    if (type === 'Map') {
        const regArr2 = newStr.match(/^([^<>]+(<.*>)?),([^<>]+(<.*>)?)$/);
        if (!regArr2) return result;

        result.$ = {
            $key: formatT(regArr2[1]),
            $value: formatT(regArr2[3]),
        }
    }

    if (type === 'List' || type === 'Array') {
        result.$ = formatT(newStr);
    }
    return result;
}

const result = formatT(str);
console.log(JSON.stringify(result, null, 4));
// Map<String, List<Map<Integer, Integer>>>
const clz = {
    $class: 'Map',
    $: {
        $key: {
            $class: 'String',
            $: null,
        },
        $value: {
            $class: 'Array',
            $: {
                $class: 'Map',
                $: {
                    $key: {
                        $class: 'Number',
                        $: null
                    },
                    $value: {
                        $class: 'Set',
                        $: {
                            $class: 'String',
                            $: null,
                        }
                    }
                }
            }
        }
    }
}

const list = [...Array(3)].map((_, i) => {
    const map = new Map();
    const set = new Set();
    set.add(i+'')
    map.set(i, set);
    return map;
});

const map = new Map();
map.set('list', list);

function type(obj) {
    return Object.prototype.toString.call(obj).slice(8, -1);
}

function formatData(data, scheme) {
    const dataScheme = JSON.parse(JSON.stringify(scheme));
    let { $class } = scheme;

    if (scheme.$class === 'T' || scheme.$class === 'Object') {
        $class === type(data);
    }

    if (type(data) !== $class) {
        console.log('-----');
        console.log(data, scheme);
        throw new Error('格式不正确');
    }

    if ($class === 'Map') {
        const arr = [];
        const { $key, $value } = scheme.$;

        for (let [key, value] of data) {
            arr.push({
                $key: formatData(key, $key),
                $value: formatData(value, $value),
            });
        }

        dataScheme.$ = arr;
    } else if ($class === 'Array' || $class === 'Set' || $class === 'List') {
        const arr = [];

        data.forEach((val) => {
            arr.push(formatData(val, scheme.$));
        });

        dataScheme.$ = arr;
    } else {
        // String, Integer, Boolean, Byte, Short
        // const typeArr = [
        //     'char', 'boolean', 'byte', 'short', 'int', 'long', 'float', 'double',
        //     'String', 'Boolean', 'Byte', 'Number',
        // ];
        // if (typeArr.indexOf($class) > -1) {}
        dataScheme.$ = data;
    }

    return dataScheme;
};

let result = formatData(map, clz);
console.log(JSON.stringify(result, null, 2));