npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-translate

v2.0.0

Published

Translates MongoDB query filters into sentences. Supports nested properties, custom field labeling, and custom locales. The result is an array of translated text for each field.

Downloads

13

Readme

Mongo Translate

Translates MongoDB query filters into sentences. Supports nested properties, custom field labeling, and custom locales. Contributions to support more locales is appreciated (currently only EN-US is supported). The result is an array of translated text for each field.

Table of Contents

v2 Changes

  • The args for the translate method have changed. It should now be called with a valid MongoDB query filter as the first argument followed by a config object that includes the labels and locale to use.
  • Improved translation for various data types.
  • Regex detection is still minimal but improved.
  • The module now exposes the reduce method which is used in the translate method but can now be called directly. This method reduces the depth of an object to 1.

Install

npm i -S mongo-translate

Usage

// Import mongo-translate
const mongoTranslate = require('mongo-translate');

// Query Filter Sample
const filter = {
  name: {
    $regex: '/^test/i'
  },
  type: {
    $ne: 'TEST'
  },
  organization: {
    $eq: 'TEST'
  },
  tags: {
    $all: ['TEST', 'ALL']
  },
  category: {
    $in: ['TEST', 'IN']
  },
  time: {
    $gt: '2021-01-01',
    $lte: '2021-01-31'
  },
  count: {
    $gte: 10,
    $lt: 20
  },
  metrics: {
    total: 1, // no operator $eq
    keys: { $in: ['nested'] },
    data: {
      nested: { $eq: 1 }
    }
  }
};

// Custom Field Labels
const labels = {
  name: 'Name',
  type: 'Type',
  organization: 'Organization',
  tags: 'Tags',
  category: 'Category',
  time: 'Date/Time',
  count: 'Count',
  'metrics.total': 'Metrics Total',
  'metrics.keys': 'Metrics Keys',
  'metrics.data.nested': 'Metrics Data Nested'
};

// Translate MongoDB query filter
mongoTranslate.translate(filter, {
  labels,
  locale: 'enus'
});

/* Output
[
  'Name is like [ ^test ]',
  'Type is not equal to [ TEST ]',
  'Organization is equal to [ TEST ]',
  'Tags includes all [ TEST, ALL ]',
  'Category includes [ TEST, IN ]',
  'Date/Time is greater than [ 12/31/2020, 6:00:00 PM ]',
  'Date/Time is less than or equal to [ 1/30/2021, 6:00:00 PM ]',
  'Count is greater than or equal to [ 10 ]',
  'Count is less than [ 20 ]',
  'Metrics Total is equal to [ 1 ]',
  'Metrics Keys includes [ nested ]',
  'Metrics Data Nested is equal to [ 1 ]'
]
*/

Options

| Property | Type | Required | Description | | -------- | ------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- | | filter | Object | Yes | MongoDB query filter | | labels | Object | No | Key:Value pairs where the Key matches an expected field or path in the MongoDB query filter, and the Value is a String to use in place of it | | locale | String | No | The locale to use when translating the query filter. Defaults to en-us (English - United States) if missing or invalid |

IMPORTANT - Not providing a label for any field that appears in the query filter will result in the path being used. If the field is nested, it will use dot notation (e.g. parent.nested-field).

Custom Locale

If the supported locale(s) are not sufficient, it is possible to use a custom one. Adding a custom locale with the same name of an existing locale will overwrite the existing one.

IMPORTANT - Each supported query filter operator (e.g. $gt, $lt, ...) should be defined in the custom locale. Be sure to include {value} where the query filter's field value should show up in the text.

In the example below, the name of the custom locale is custom but it could be anything (e.g. sarcastic, lame, hopeful, sullen).

// Import mongo-translate
const mongoTranslate = require('mongo-translate');

// Define custom locale
const customLocale = {
  $gt: 'CUSTOM GT LOCALE {value}',
  $gte: 'CUSTOM GTE LOCALE {value}',
  $lt: 'CUSTOM LT LOCALE {value}',
  $lte: 'CUSTOM LTE LOCALE  {value}',
  $in: 'CUSTOM IN LOCALE {value}',
  $nin: 'CUSTOM NIN LOCALE {value}',
  $all: 'CUSTOM ALL LOCALE {value}',
  $eq: 'CUSTOM EQ LOCALE {value}',
  $ne: 'CUSTOM NE LOCALE {value}',
  $regex: 'CUSTOM REGEX LOCALE {value}',
};
// Add custom locale to the mongo-translate's locales
mongoTranslate.locales.custom = customLocale;

const filter = {...}; // same as defined in Usage
const labels = {...}; // same as defined in Usage

// Translate MongoDB query filter
mongoTranslate.translate({
    filter,
    labels,
    locale: 'custom'
})

/* Output
[
  'Name CUSTOM REGEX LOCALE [ ^test ]',
  'Type CUSTOM NE LOCALE [ TEST ]',
  'Organization CUSTOM EQ LOCALE [ TEST ]',
  'Tags CUSTOM ALL LOCALE [ TEST, ALL ]',
  'Category CUSTOM IN LOCALE [ TEST, IN ]',
  'Date/Time CUSTOM GT LOCALE [ 12/31/2020, 6:00:00 PM ]',
  'Date/Time CUSTOM LTE LOCALE  [ 1/30/2021, 6:00:00 PM ]',
  'Count CUSTOM GTE LOCALE [ 10 ]',
  'Count CUSTOM LT LOCALE [ 20 ]',
  'Metrics Total CUSTOM EQ LOCALE [ 1 ]',
  'Metrics Keys CUSTOM IN LOCALE [ nested ]',
  'Metrics Data Nested CUSTOM EQ LOCALE [ 1 ]'
]
*/

Reduce

const obj = {
  propA: 1,
  propB: {
    nestedPropA: 1,
    nestedPropB: ['a', 'b', 'c']
  }
  propC: [{propA: 1, propB: 2}, {propA: 1, propB: 2}]
}

// Reduce without reducing arrays
reduce(obj)

/* Output
{
  propA: 1,
  'propB.nestedPropA': 1,
  'propB.nestedPropB': [ 'a', 'b', 'c' ],
  propC: [ { propA: 1, propB: 2 }, { propA: 1, propB: 2 } ]
}
*/