Code Monkey home page Code Monkey logo

dynamic-parsers's Introduction

Installation

npm install dynamic-parsers --save

Multi-Platform Strings Files Manager

Define Translations Data Structure

// Platform is an exported enum in our StringsGenerator Class:
// export enum Platform { WEB = 0, IOS = 1, ANDROID = 2, API = 3 }

const translations = [
  { key: 'HELLO', translations: { en: 'Hello English', fr: 'Hello French' }, platforms: [0, 1, 2] },
  { key: 'ERROR', translations: { en: 'Error English', fr: 'Error French' }, platforms: [0, 1, 2] },
  { key: 'EMAIL_INUSE', translations: { en: 'Email in use English', fr: 'Email in use French' }, platforms: [1, 2] },
];

Setup Generator

const { StringsGenerator } = require('dynamic-parsers');

const generator = new StringsGenerator(translations, ['en', 'fr']);

Option 1 - Write to a Local File

// async/await
(async() => {

  const platform = 1; // = Platform.IOS
  const zipFile = await generator.generateZip(platform);
  fs.writeFileSync('file.zip', zipFile);
  
})();


// Promise
generator.generateZip(platform).then(zipFile => {

  const platform = 2; // = Platform.ANDROID
  fs.writeFileSync('file.zip', zipFile);

});

Option 2 - Serve in API using a Simple Express Server

app.get('/generator', async (req, res) => {

  const platform = 0; // = Platform.WEB
  const zipFile = await generator.generateZip(platform);

  res.writeHead(200, {
    'Content-Type': 'application/zip',
    'Content-Length': zipFile.length
  });
  
  return res.end(zipFile);
});

Class Parameters

const generator = new StringsGenerator(translations, languages)

param default type
translations (required) Translations
languages (required) string[]

Translations Interface

export enum Platform { WEB = 0, IOS = 1, ANDROID = 2, API = 3 }

export interface Translations {
  readonly translations: { [key: string]: string }; // e.g. { en: 'Good Morning', fr: 'Bonjour' }
  readonly translatable?: boolean;
  readonly platforms: Platform[]; // e.g. [1, 2, 3]
  readonly key: string;
}

Web Crawler and Scraper

const crawlerConfig = {
  url: 'https://www.example.com/search/result?q=macbook',
  itemSelector: 'ul.products-grid .item',
  itemDetails: [
    { key: 'name', selector: '.product-name' },
    { key: 'sale', selector: '.product-sale .price' },
    { key: 'price', selector: '.product-details .price' },
    { key: 'image', selector: '.product-image img', attribute: 'data-src' },
  ]
};

(async () => {
  const data = await WebCrawl(crawlerConfig);
  console.log("LOG: data", data);
})();

Crawler Config Structure

// either "html" or "url" is required

export interface ItemDetailsConfig {
  key: string;
  selector: string;
  attribute?: string;
}

export interface CrawlerConfig {
  url?: string;
  html?: string;
  itemSelector: string;
  itemDetails: ItemDetailsConfig[];
}

Contact Author: Serge Harb

dynamic-parsers's People

Contributors

surgeharb avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

oluagunloye

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.