util#isArray TypeScript Examples

The following examples show how to use util#isArray. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: extension.ts    From keil-assistant with MIT License 6 votes vote down vote up
async load() {

        const parser = new xml2js.Parser({ explicitArray: false });
        const doc = await parser.parseStringPromise({ toString: () => { return this.uvprjFile.Read(); } });
        const targets = doc['Project']['Targets']['Target'];

        // init uVsion info
        this.uVsionFileInfo.schemaVersion = doc['Project']['SchemaVersion'];

        if (isArray(targets)) {
            for (const target of targets) {
                this.targetList.push(Target.getInstance(this, this.uVsionFileInfo, target));
            }
        } else {
            this.targetList.push(Target.getInstance(this, this.uVsionFileInfo, targets));
        }

        for (const target of this.targetList) {
            await target.load();
            target.on('dataChanged', () => this.notifyUpdateView());
        }
    }
Example #2
Source File: parser.ts    From vscode-rss with MIT License 6 votes vote down vote up
function parseLink(link: any) {
    if (isArray(link) && link.length > 0) {
        link = link.reduce((a, b) => order(a.__attr) > order(b.__attr) ? a : b);
    }

    let ans;
    if (isStringified(link)) {
        ans = link;
    } else if (isStringified(link.__attr?.href)) {
        ans = link.__attr.href;
    } else if (isStringified(link.__text)) {
        ans = link.__text;
    } else if ('__cdata' in link) {
        if (isStringified(link.__cdata)) {
            ans = link.__cdata;
        } else if(isArray(link.__cdata)) {
            ans = link.__cdata.join('');
        }
    }
    return ans;
}
Example #3
Source File: parser.ts    From vscode-rss with MIT License 6 votes vote down vote up
function dom2html(name: string, node: any) {
    if (isStringified(node)) {
        return `<${name}>${node}</${name}>`;
    }

    let html = '<' + name;
    if ('__attr' in node) {
        for (const key in node.__attr) {
            const value = node.__attr[key];
            html += ` ${key}="${value}"`;
        }
    }
    html += '>';

    if (isStringified(node.__text)) {
        html += node.__text;
    }
    for (const key in node) {
        if (key.startsWith('__')) {continue;}
        const value = node[key];
        if (isArray(value)) {
            for (const item of value) {
                html += dom2html(key, item);
            }
        } else {
            html += dom2html(key, value);
        }
    }
    html += `</${name}>`;
    return html;
}
Example #4
Source File: parser.ts    From vscode-rss with MIT License 6 votes vote down vote up
function extractText(content: any) {
    let ans;
    if (isStringified(content)) {
        ans = content;
    } else if (isStringified(content.__text)) {
        ans = content.__text;
    } else if ('__cdata' in content) {
        if (isStringified(content.__cdata)) {
            ans = content.__cdata;
        } else if(isArray(content.__cdata)) {
            ans = content.__cdata.join('');
        }
    } else if (content.__attr?.type === 'html') {
        // XXX: temporary solution. convert dom object to html string.
        ans = dom2html('html', content);
    }
    return ans;
}
Example #5
Source File: extension.ts    From keil-assistant with MIT License 4 votes vote down vote up
async load(): Promise<void> {

        // check target is valid
        const err = this.checkProject(this.targetDOM);
        if (err) { throw err; }

        const incListStr: string = this.getIncString(this.targetDOM);
        const defineListStr: string = this.getDefineString(this.targetDOM);
        const _groups: any = this.getGroups(this.targetDOM);
        const sysIncludes = this.getSystemIncludes(this.targetDOM);

        // set includes
        this.includes.clear();

        let incList = incListStr.split(';');
        if (sysIncludes) {
            incList = incList.concat(sysIncludes);
        }

        incList.forEach((path) => {
            const realPath = path.trim();
            if (realPath !== '') {
                this.includes.add(this.project.toAbsolutePath(realPath));
            }
        });

        // set defines
        this.defines.clear();

        // add user macros
        defineListStr.split(/,|\s+/).forEach((define) => {
            if (define.trim() !== '') {
                this.defines.add(define);
            }
        });

        // add system macros
        this.getSysDefines(this.targetDOM).forEach((define) => {
            this.defines.add(define);
        });

        // set file groups
        this.fGroups = [];

        let groups: any[];
        if (Array.isArray(_groups)) {
            groups = _groups;
        } else {
            groups = [_groups];
        }

        for (const group of groups) {

            if (group['Files'] !== undefined) {

                let isGroupExcluded = false;
                let fileList: any[];

                if (group['GroupOption']) { // check group is excluded
                    const gOption = group['GroupOption']['CommonProperty'];
                    if (gOption && gOption['IncludeInBuild'] === '0') {
                        isGroupExcluded = true;
                    }
                }

                const nGrp = new FileGroup(this.prjID, group['GroupName'], isGroupExcluded);

                if (Array.isArray(group['Files'])) {
                    fileList = [];
                    for (const files of group['Files']) {
                        if (Array.isArray(files['File'])) {
                            fileList = fileList.concat(files['File']);
                        }
                        else if (files['File'] !== undefined) {
                            fileList.push(files['File']);
                        }
                    }
                } else {
                    if (Array.isArray(group['Files']['File'])) {
                        fileList = group['Files']['File'];
                    }
                    else if (group['Files']['File'] !== undefined) {
                        fileList = [group['Files']['File']];
                    } else {
                        fileList = [];
                    }
                }

                for (const file of fileList) {
                    const f = new File(this.project.toAbsolutePath(file['FilePath']));

                    let isFileExcluded = isGroupExcluded;
                    if (isFileExcluded === false && file['FileOption']) { // check file is enable
                        const fOption = file['FileOption']['CommonProperty'];
                        if (fOption && fOption['IncludeInBuild'] === '0') {
                            isFileExcluded = true;
                        }
                    }

                    const nFile = new Source(this.prjID, f, !isFileExcluded);
                    this.includes.add(f.dir);
                    nGrp.sources.push(nFile);
                }

                this.fGroups.push(nGrp);
            }
        }

        this.updateCppProperties();

        this.updateSourceRefs();
    }
Example #6
Source File: parser.ts    From vscode-rss with MIT License 4 votes vote down vote up
export function parseXML(xml: string, exclude: Set<string>): [Entry[], Summary] {
    const match = xml.match(/<\?xml.*encoding="(\S+)".*\?>/);
    xml = iconv.decode(Buffer.from(xml, 'binary'), match ? match[1]: 'utf-8');
    const dom = parser.parse(xml, {
        attributeNamePrefix: "",
        attrNodeName: "__attr",
        textNodeName: "__text",
        cdataTagName: "__cdata",
        cdataPositionChar: "",
        ignoreAttributes: false,
        parseAttributeValue: true,
    });
    let feed;
    if (dom.rss) {
        if (dom.rss.channel) {
            feed = dom.rss.channel;
        } else if (dom.rss.feed) {
            feed = dom.rss.feed;
        }
    } else if (dom.channel) {
        feed = dom.channel;
    } else if (dom.feed) {
        feed = dom.feed;
    } else if (dom["rdf:RDF"]) {
        feed = dom["rdf:RDF"];
    }
    if (!feed) {
        throw new Error('Feed Format Error');
    }

    let title;
    if ('title' in feed) {
        title = extractText(feed.title);
    } else if (feed.channel?.title !== undefined) {
        title = extractText(feed.channel.title);
    }
    if (!isStringified(title)) {
        throw new Error('Feed Format Error: Missing Title');
    }
    title = he.decode(title);

    let link: any;
    if (feed.link) {
        link = parseLink(feed.link);
    } else if (feed.channel?.link) {
        link = parseLink(feed.channel.link);
    }
    if (!isStringified(link)) {
        throw new Error('Feed Format Error: Missing Link');
    }
    if (!link.match(/^https?:\/\//)) {
        if (link.match(/^\/\//)) {
            link = 'http:' + link;
        } else {
            link = 'http://' + link;
        }
    }

    let items: any;
    if (feed.item) {
        items = feed.item;
    } else if (feed.entry) {
        items = feed.entry;
    }
    if (!items) {
        items = [];
    } else if (!isArray(items)) {
        items = [items];
    }

    const entries: Entry[] = [];
    for (const item of items) {
        const entry = parseEntry(item, link, exclude);
        if (entry) {
            entries.push(entry);
        }
    }
    const summary = new Summary(link, title);

    return [entries, summary];
}