Skip to content

Commit

Permalink
style: refactor to the preffered wfcd style rules
Browse files Browse the repository at this point in the history
  • Loading branch information
AyAyEm committed Nov 28, 2022
1 parent cf46e22 commit 9fdce35
Show file tree
Hide file tree
Showing 23 changed files with 54 additions and 97 deletions.
8 changes: 4 additions & 4 deletions build/build.mjs
Expand Up @@ -7,10 +7,10 @@ import fetch from 'node-fetch';
import sharp from 'sharp';

import Progress from './progress.mjs';
import { stringify } from './stringify.mjs';
import { scraper } from './scraper.mjs';
import { parser } from './parser.mjs';
import { hashManager } from './hashManager.mjs';
import stringify from './stringify.mjs';
import scraper from './scraper.mjs';
import parser from './parser.mjs';
import hashManager from './hashManager.mjs';
import imagesCache from '../data/cache/.images.json' assert { type: 'json' };
import exportCache from '../data/cache/.export.json' assert { type: 'json' };

Expand Down
6 changes: 2 additions & 4 deletions build/dedupe.mjs
@@ -1,11 +1,11 @@
import { sortObject } from './sortObject.mjs';
import sortObject from './sortObject.mjs';

/**
* Simple deduplication leveraging reduce (and equality based on stringification)
* @param {Iterable<*>} iter Should be an iterable object, but function will check first.
* @returns {Iterable<*>} type will be whatever was originally passed in
*/
export function dedupe(iter) {
export default (iter) => {
return Array.isArray(iter)
? iter
.reduce(
Expand All @@ -17,5 +17,3 @@ export function dedupe(iter) {
.map((o) => JSON.parse(o))
: iter;
}

export default dedupe;
8 changes: 3 additions & 5 deletions build/hashManager.mjs
@@ -1,12 +1,12 @@
import fs from 'fs/promises';

import { scraper } from './scraper.mjs';
import scraper from './scraper.mjs';
import exportCache from '../data/cache/.export.json' assert { type: 'json' };
import locales from '../config/locales.json' assert { type: 'json' };

const exportKeyWhitelist = ['Manifest', 'DropChances', 'Patchlogs'];

export class HashManager {
class HashManager {
constructor() {
this.exportCache = Object.fromEntries(
exportKeyWhitelist.filter((key) => key in exportCache).map((key) => [key, exportCache[key]])
Expand Down Expand Up @@ -53,6 +53,4 @@ export class HashManager {
}
}

export const hashManager = new HashManager();

export default hashManager;
export default new HashManager();
10 changes: 4 additions & 6 deletions build/parser.mjs
@@ -1,8 +1,8 @@
import _ from 'lodash';

import Progress from './progress.mjs';
import { dedupe } from './dedupe.mjs';
import { isTradable } from './tradable.mjs';
import dedupe from './dedupe.mjs';
import isTradable from './tradable.mjs';
import previousItems from '../data/json/All.json' assert { type: 'json' };
import watson from '../config/dt_map.json' assert { type: 'json' };
import bpConflicts from '../config/bpConflicts.json' assert { type: 'json' };
Expand Down Expand Up @@ -96,7 +96,7 @@ const dropMap = (drop) => {
/**
* Parse API data into a more clear or complete format.
*/
export class Parser {
class Parser {
/**
* @typedef {Object} ParsedData
* @property {Array<Item>} data
Expand Down Expand Up @@ -1083,6 +1083,4 @@ export class Parser {
}
}

export const parser = new Parser();

export default parser;
export default new Parser();
12 changes: 7 additions & 5 deletions build/scraper.mjs
Expand Up @@ -7,7 +7,11 @@ import { load } from 'cheerio';

import { Generator as RelicGenerator } from '@wfcd/relics';

import { ModScraper, WeaponScraper, WarframeScraper, VersionScraper } from './wikia/scrapers/index.mjs';
import ModScraper from './wikia/scrapers/ModScraper.mjs';
import WeaponScraper from './wikia/scrapers/WeaponScraper.mjs';
import WarframeScraper from './wikia/scrapers/WarframeScraper.mjs';
import VersionScraper from './wikia/scrapers/VersionScraper.mjs';

import Progress from './progress.mjs';
import exportCache from '../data/cache/.export.json' assert { type: 'json' };
import locales from '../config/locales.json' assert { type: 'json' };
Expand All @@ -34,7 +38,7 @@ const getJSON = async (url, disableProxy) => JSON.parse(sanitize(await get(url,
/**
* Retrieves the base item data necessary for the parsing process
*/
export class Scraper {
class Scraper {
/**
* Get Endpoints from Warframe's origin file
* @param {boolean} [manifest] to fetch only the manifest or everything else
Expand Down Expand Up @@ -288,6 +292,4 @@ export class Scraper {
}
}

export const scraper = new Scraper();

export default scraper;
export default new Scraper();
6 changes: 2 additions & 4 deletions build/sortObject.mjs
Expand Up @@ -4,13 +4,11 @@
* @param {Record<string, *>} obj object to be sorted
* @returns {Record<string, *>} same as {@param obj} but sorted keys
*/
export function sortObject(obj) {
export default (obj) => {
return Object.keys(obj)
.sort()
.reduce(function (result, key) {
result[key] = obj[key];
return result;
}, {});
}

export default sortObject;
};
6 changes: 2 additions & 4 deletions build/stringify.mjs
@@ -1,4 +1,4 @@
import { dedupe } from './dedupe.mjs';
import dedupe from './dedupe.mjs';

/**
* Pretty print JSON as it should be.
Expand All @@ -11,6 +11,4 @@ const isArrayOfPrimitive = (obj) => Array.isArray(obj) && obj.every(isPrimitive)
const format = (arr) => `^^^[ ${arr.map((val) => JSON.stringify(val)).join(', ')} ]`;
const replacer = (key, value) => (isArrayOfPrimitive(value) ? format(value) : value);
const expand = (str) => str.replace(/"\^\^\^(\[ .* ])"/g, (match, a) => a.replace(/\\"/g, '"')).replace(/\\\\"/g, "'");
export const stringify = (obj) => expand(JSON.stringify(Array.isArray(obj) ? dedupe(obj) : obj, replacer, 2));

export default stringify;
export default (obj) => expand(JSON.stringify(Array.isArray(obj) ? dedupe(obj) : obj, replacer, 2));
6 changes: 2 additions & 4 deletions build/tradable.mjs
Expand Up @@ -49,7 +49,7 @@ const untradableRegex =
* @param {module:warframe-items.Item} item Item to determine tradability
* @returns {boolean}
*/
export function isTradable(item) {
export default (item) => {
const notFiltered =
!untradableTypes.includes(item.type) &&
!item.name.match(untradableRegex) &&
Expand All @@ -59,6 +59,4 @@ export function isTradable(item) {
const tradableByName = !!((item.uniqueName.match(tradableRegex) || item.name.match(tradableRegex)) && notFiltered);
const tradableByProp = !!(item.isAugment && notFiltered);
return (tradableByType || tradableByName || tradableByProp) && tradableConditions(item);
}

export default isTradable;
};
8 changes: 2 additions & 6 deletions build/wikia/WikiaDataScraper.mjs
Expand Up @@ -6,9 +6,7 @@ import { promisify } from 'util';
import nodeCmd from 'node-cmd';
import fetch, { Request } from 'node-fetch';

const { get } = nodeCmd;

const run = promisify(get);
const run = promisify(nodeCmd.get);

const blueprintUrl = 'https://warframe.fandom.com/wiki/Module:Blueprints/data?action=edit';

Expand Down Expand Up @@ -121,7 +119,7 @@ let blueprints;
/**
* Scrape Wikia data from data modules
*/
export class WikiaDataScraper {
export default class WikiaDataScraper {
constructor(url, luaObjectName, transformFunction) {
if (Array.isArray(url)) {
this.urls = url;
Expand Down Expand Up @@ -190,5 +188,3 @@ export class WikiaDataScraper {
return things;
}
}

export default WikiaDataScraper;
8 changes: 3 additions & 5 deletions build/wikia/scrapers/ModScraper.mjs
@@ -1,10 +1,8 @@
import { WikiaDataScraper } from '../WikiaDataScraper.mjs';
import { transformMod } from '../transformers/index.mjs';
import WikiaDataScraper from '../WikiaDataScraper.mjs';
import transformMod from '../transformers/transformMod.mjs';

export class ModScraper extends WikiaDataScraper {
export default class ModScraper extends WikiaDataScraper {
constructor() {
super('https://warframe.fandom.com/wiki/Module:Mods/data?action=edit', 'Mod', transformMod);
}
}

export default ModScraper;
8 changes: 3 additions & 5 deletions build/wikia/scrapers/VersionScraper.mjs
@@ -1,10 +1,8 @@
import { WikiaDataScraper } from '../WikiaDataScraper.mjs';
import { transformVersion } from '../transformers/index.mjs';
import WikiaDataScraper from '../WikiaDataScraper.mjs';
import transformVersion from '../transformers/transformVersion.mjs';

export class VersionScraper extends WikiaDataScraper {
export default class VersionScraper extends WikiaDataScraper {
constructor() {
super('https://warframe.fandom.com/wiki/Module:Version/data?action=edit', 'Version', transformVersion);
}
}

export default VersionScraper;
8 changes: 3 additions & 5 deletions build/wikia/scrapers/WarframeScraper.mjs
@@ -1,10 +1,8 @@
import { WikiaDataScraper } from '../WikiaDataScraper.mjs';
import { transformWarframe } from '../transformers/index.mjs';
import WikiaDataScraper from '../WikiaDataScraper.mjs';
import transformWarframe from '../transformers/transformWarframe.mjs';

export class WarframeScraper extends WikiaDataScraper {
export default class WarframeScraper extends WikiaDataScraper {
constructor() {
super('https://warframe.fandom.com/wiki/Module:Warframes/data?action=edit', 'Warframe', transformWarframe);
}
}

export default WarframeScraper;
8 changes: 3 additions & 5 deletions build/wikia/scrapers/WeaponScraper.mjs
@@ -1,11 +1,11 @@
import { WikiaDataScraper } from '../WikiaDataScraper.mjs';
import { transformWeapon } from '../transformers/index.mjs';
import WikiaDataScraper from '../WikiaDataScraper.mjs';
import transformWeapon from '../transformers/transformWeapon.mjs';

const base = 'https://warframe.fandom.com/wiki/Module:Weapons/data';
const suffix = '?action=edit';
const subModules = ['archwing', 'companion', 'melee', 'misc', 'modular', 'primary', 'secondary', 'railjack'];

export class WeaponScraper extends WikiaDataScraper {
export default class WeaponScraper extends WikiaDataScraper {
constructor() {
super(
subModules.map((subModule) => `${base}/${subModule}${suffix}`),
Expand All @@ -14,5 +14,3 @@ export class WeaponScraper extends WikiaDataScraper {
);
}
}

export default WeaponScraper;
4 changes: 0 additions & 4 deletions build/wikia/scrapers/index.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion build/wikia/transformers/elements.mjs
@@ -1,4 +1,4 @@
export const ELEMENTS = {
const ELEMENTS = {
Electricity: 'electricity',
Corrosive: 'corrosive',
Toxin: 'toxin',
Expand Down
7 changes: 0 additions & 7 deletions build/wikia/transformers/index.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion build/wikia/transformers/polarities.mjs
@@ -1,4 +1,4 @@
export const POLARITIES = {
const POLARITIES = {
Bar: 'naramon',
V: 'madurai',
D: 'vazarin',
Expand Down
4 changes: 1 addition & 3 deletions build/wikia/transformers/transformMod.mjs
@@ -1,4 +1,4 @@
export async function transformMod(oldMod, imageUrls) {
export default async (oldMod, imageUrls) => {
let newMod;
if (!oldMod || !oldMod.Name) {
return undefined;
Expand All @@ -20,5 +20,3 @@ export async function transformMod(oldMod, imageUrls) {
}
return newMod;
}

export default transformMod;
6 changes: 2 additions & 4 deletions build/wikia/transformers/transformPolarity.mjs
@@ -1,4 +1,4 @@
import { POLARITIES } from './polarities.mjs';
import POLARITIES from './polarities.mjs';

const transform = (field) => {
let output;
Expand All @@ -19,13 +19,11 @@ const transform = (field) => {
* @param {Array<string>} [Polarities] list of strings designating polarities on a weapon or warframe
* @returns {*}
*/
export function transformPolarity({ AuraPolarity, StancePolarity, Polarity, Polarities }, target) {
export default ({ AuraPolarity, StancePolarity, Polarity, Polarities }, target) => {
const output = { ...target };
output.auraPolarity = transform(AuraPolarity);
output.stancePolarity = transform(StancePolarity);
output.polarity = transform(Polarity);
output.polarities = Polarities && Polarities.length ? Polarities.map(transform) : undefined;
return output;
}

export default transformPolarity;
4 changes: 1 addition & 3 deletions build/wikia/transformers/transformVersion.mjs
@@ -1,4 +1,4 @@
export async function transformVersion(oldVersion) {
export default async (oldVersion) => {
let newVersion;

if (!oldVersion || !oldVersion.Name) {
Expand All @@ -21,5 +21,3 @@ export async function transformVersion(oldVersion) {
}
return newVersion;
}

export default transformVersion;
10 changes: 4 additions & 6 deletions build/wikia/transformers/transformWarframe.mjs
@@ -1,7 +1,7 @@
import getColors from 'get-image-colors';
import { image as _image } from 'image-downloader';
import { image as getImage } from 'image-downloader';

import { transformPolarity } from './transformPolarity.mjs';
import transformPolarity from './transformPolarity.mjs';

const mapColors = async (oldFrame, imageUrl) => {
if (!imageUrl) return 0;
Expand All @@ -11,7 +11,7 @@ const mapColors = async (oldFrame, imageUrl) => {
url: imageUrl,
dest: `tmp/tmp-${oldFrame.Name}.png`,
};
const { image } = await _image(options);
const { image } = await getImage(options);
const colors = await getColors(image, 'image/png');
return typeof colors !== 'undefined' ? colors[0].hex().replace('#', '0x') : 0xff0000;
} catch (e) {
Expand All @@ -27,7 +27,7 @@ const mapColors = async (oldFrame, imageUrl) => {
* @param {Record<string, Object>} blueprints blueprint objects
* @returns {Promise<WikiaWarframe>}
*/
export async function transformWarframe(oldFrame, imageUrls, blueprints) {
export default async (oldFrame, imageUrls, blueprints) => {
let newFrame;
if (!oldFrame || !oldFrame.Name) {
return undefined;
Expand Down Expand Up @@ -62,5 +62,3 @@ export async function transformWarframe(oldFrame, imageUrls, blueprints) {
}
return newFrame;
}

export default transformWarframe;
8 changes: 3 additions & 5 deletions build/wikia/transformers/transformWeapon.mjs
@@ -1,5 +1,5 @@
import { ELEMENTS } from './elements.mjs';
import { transformPolarity } from './transformPolarity.mjs';
import ELEMENTS from './elements.mjs';
import transformPolarity from './transformPolarity.mjs';

const damageTypes = [
'Impact',
Expand Down Expand Up @@ -72,7 +72,7 @@ const parseSlam = ({ SlamAttack, SlamRadialDmg, SlamRadialElement, SlamRadialPro
};
};

export function transformWeapon(oldWeapon, imageUrls, blueprints) {
export default (oldWeapon, imageUrls, blueprints) => {
let newWeapon;
if (!oldWeapon || !oldWeapon.Name) {
return undefined;
Expand Down Expand Up @@ -172,5 +172,3 @@ export function transformWeapon(oldWeapon, imageUrls, blueprints) {

return newWeapon;
}

export default transformWeapon;
2 changes: 1 addition & 1 deletion test/index.spec.mjs
Expand Up @@ -2,7 +2,7 @@ import assert from 'node:assert';
import { resolve } from 'node:path';
import { createRequire } from 'module';

import { dedupe } from '../build/dedupe.mjs';
import dedupe from '../build/dedupe.mjs';

const require = createRequire(import.meta.url);

Expand Down

0 comments on commit 9fdce35

Please sign in to comment.