Skip to content

Commit

Permalink
Merge pull request #79 from Unibeautify/unibeautify/dependency-manage…
Browse files Browse the repository at this point in the history
…r/push

🎨 Fix formatting of 2 files for commit e83cb51
  • Loading branch information
Glavin001 committed Apr 10, 2018
2 parents e83cb51 + d5ad775 commit 7c9812e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 47 deletions.
98 changes: 59 additions & 39 deletions src/beautifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,25 @@ export class Unibeautify {
const langs: Language[] = [
...this.languages.filter(lang => lang.name === query.name),
...this.languages.filter(lang => lang.namespace === query.namespace),
...this.languages.filter(lang => query.extension && lang.extensions.indexOf(query.extension) !== -1),
...this.languages.filter(lang => query.atomGrammar && lang.atomGrammars.indexOf(query.atomGrammar) !== -1),
...this.languages.filter(lang => query.sublimeSyntax && lang.sublimeSyntaxes.indexOf(query.sublimeSyntax) !== -1),
...this.languages.filter(lang => query.vscodeLanguage && lang.vscodeLanguages.indexOf(query.vscodeLanguage) !== -1),
...this.languages.filter(
lang =>
query.extension && lang.extensions.indexOf(query.extension) !== -1
),
...this.languages.filter(
lang =>
query.atomGrammar &&
lang.atomGrammars.indexOf(query.atomGrammar) !== -1
),
...this.languages.filter(
lang =>
query.sublimeSyntax &&
lang.sublimeSyntaxes.indexOf(query.sublimeSyntax) !== -1
),
...this.languages.filter(
lang =>
query.vscodeLanguage &&
lang.vscodeLanguages.indexOf(query.vscodeLanguage) !== -1
),
];

return unique<Language>(langs);
Expand All @@ -458,17 +473,17 @@ export class Unibeautify {
* Get first loaded beautifier for given language.
*/
private getBeautifierForLanguage(language: Language): Beautifier | undefined {
return this.beautifiers.find(
beautifier => this.doesBeautifierSupportLanguage(beautifier, language)
return this.beautifiers.find(beautifier =>
this.doesBeautifierSupportLanguage(beautifier, language)
);
}

/**
* Find and return the appropriate Beautifiers for the given Language.
*/
public getBeautifiersForLanguage(language: Language): Beautifier[] {
return this.beautifiers.filter(
beautifier => this.doesBeautifierSupportLanguage(beautifier, language)
return this.beautifiers.filter(beautifier =>
this.doesBeautifierSupportLanguage(beautifier, language)
);
}

Expand Down Expand Up @@ -562,37 +577,42 @@ export class Unibeautify {
return {};
}
} else if (typeof beautifierOptions === "object") {
return Object.keys(beautifierOptions).reduce((acc: OptionValues, key: string) => {
const option = beautifierOptions[key];
if (typeof option === "string") {
return {
...acc,
[key]: options[option],
};
} else if (typeof option === "function") {
return {
...acc,
[key]: option(options[key]),
};
} else if (option === true) {
return {
...acc,
[key]: options[key],
};
} else if (option instanceof Array) {
const [fields, fn] = option;
const values = fields.map(field => options[field]);
const obj = zipObject(fields, values);
return {
...acc,
[key]: fn(obj),
};
}

// tslint:disable-next-line
console.log(`Invalid option "${key}" with value ${JSON.stringify(option)}.`);
return acc;
}, {} as OptionValues);
return Object.keys(beautifierOptions).reduce(
(acc: OptionValues, key: string) => {
const option = beautifierOptions[key];
if (typeof option === "string") {
return {
...acc,
[key]: options[option],
};
} else if (typeof option === "function") {
return {
...acc,
[key]: option(options[key]),
};
} else if (option === true) {
return {
...acc,
[key]: options[key],
};
} else if (option instanceof Array) {
const [fields, fn] = option;
const values = fields.map(field => options[field]);
const obj = zipObject(fields, values);
return {
...acc,
[key]: fn(obj),
};
}

// tslint:disable-next-line
console.log(
`Invalid option "${key}" with value ${JSON.stringify(option)}.`
);
return acc;
},
{} as OptionValues
);
} else {
return options;
}
Expand Down
19 changes: 11 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
export function zipObject(keys: string[], values: string[]) {
return keys.reduce((acc, key, idx) => ({
...acc,
[key]: values[idx],
}), {});
return keys.reduce(
(acc, key, idx) => ({
...acc,
[key]: values[idx],
}),
{}
);
}

export function unique<T>(array: T[]): T[] {
return array.reduce((acc, val) => acc.indexOf(val) === -1
? acc.concat(val)
: acc
, [] as T[]);
return array.reduce(
(acc, val) => (acc.indexOf(val) === -1 ? acc.concat(val) : acc),
[] as T[]
);
}

0 comments on commit 7c9812e

Please sign in to comment.