1.0.73 • Published 7 months ago

@brutdecom/bdc_common v1.0.73

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

CONTENTS OF THIS PACKAGE

Introduction

made-with-javascript

This package is npm package in JavaScript for BrutDeCom projects.

Installation

For install this package, run npm i @brutdecom/bdc_common

Using

Example for require one part of this package with "Regex" part :

const { Regex } = require('@brutdecom/bdc_common')

or

import { Regex } from @brutdecom/bdc_common

Regex

Example for use Regex with this package :

// Require Regex part of this package
const { Regex } = require('@brutdecom/bdc_common')

// or

import { Regex } from '@brutdecom/bdc_common'

// Use password regex
const myConst = Regex.password
// return regex
Regex TypeDescription
password8 characters, 1 lowercase, 1 uppercase, 1 special character, 1 number
phonePhone and fax number
cityCity (2 characters minimum, not number)
zipFrench zip code (5 numbers)

Validator

Validator part is utils for validate various data. Example for use Validator with this package :

// Require Validator part of this package
const { Validator } = require('@brutdecom/bdc_common')

// or

import { Validator } from '@brutdecom/bdc_common'

// Use isValidString for example
const myConst = Validator.isValidString('string')
// return true

const myConst = Validator.isValidString(14)
// return false
NameDescriptionParametersreturn
isValidString('string')Verify if value is valid stringString parameters ('one string')Return true or false
isValidEmail('email@test.com')Verify if email is valid formatString parameters ('email@test.com')Return true or false
isValidZip('40500')Verify if value is valid zip code format for France.String parameters ('40530')Return true or false
isValidCountry('France')Verify if value is valid country format.String parameters ('France')Return true or false
isValidAddress('3 rue de la Liberté')Verify if value is valid address format.String parameters ('3 rue de la Liberté')Return true or false
isValidCity('Biarritz')Verify if value is valid city format.String parameters ('Biarritz')Return true or false
isValidPhone('0625458769')Verify if value is valid phone and fax number format.String parameters ('0625458769')Return true or false
isValidLastname('Lastname')Verify if value is valid lastname format.String parameters ('Lastname')Return true or false
isValidFirstname('Firstname')Verify if value is valid Firstname format.String parameters ('Firstname')Return true or false
isValidPassword('password')Verify if value is valid password format (8 characters, 1 lowercase, 1 uppercase, 1 special character, 1 number).String parameters ('password')Return true or false
isValidSiret('string')Verify if value is valid siretString siret parameters ('12345678998765')Return true or false
isValidEnum('string', array)Verify if value is valid enumerationString value parameters ('test'), array parameters (Enum.MyEnum)Return true or false

YUP Validator for Formik (InputValidator)

InputValidator part is utils for validate data in Formik form (validationSchemas parameter). Validate data, and display matches error in frontend (based on YUP package)

Example for use InputValidator with this package :

// Require InputValidator part of this package
const { InputValidator, FormValidator } = require('@brutdecom/bdc_common')

// or

import { InputValidator, FormValidator } from '@brutdecom/bdc_common'

// My form contain two fields, "email" and "password" for auth. I want validate field data, use InputValidator with FormValidator.mergedYupSchemas
// This variable contain my validationSchema object
// mergedYupSchemas is utils function for grouped multiple validator in schema object, is required
const validationSchema = FormValidator.mergedYupSchemas(InputValidator.email, InputValidator.password)

// After, just passed validationSchema in Form

  const formik = useFormik({
    initialValues: initialValues,
    validationSchema: validationSchema,
    onSubmit: values => {
      action(values)
    }
  })

  ......
NameDescription
InputValidator.emailVerify if value is valid email
InputValidator.passwordVerify if value is valid password
InputValidator.firstnameFirstname field validation
InputValidator.lastnameLastname field validation
InputValidator.passwordConfirmpasswordConfirm field validation, for verify identical password in form
InputValidator.cityCity field validation
InputValidator.zipCodezipCode field validation
InputValidator.addressaddress field validation
InputValidator.phonePhone field validation
InputValidator.faxFax field validation
InputValidator.countryCountry field validation
InputValidator.companyCompany field validation
InputValidator.roleRole field validation
InputValidator.accommodationAccommodation field validation
InputValidator.userTypeuserType field validation
InputValidator.socialReasonenterprise socialReason field validation
InputValidator.siretsiret field validation
InputValidator.capebcapeb field validation
InputValidator.activityTypeactivityType field validation
InputValidator.accommodationNameaccommodationName field validation
InputValidator.birthdatebirthdate field validation
InputValidator.birthCountrybirthCountry field validation
InputValidator.birthCountybirthCounty field validation
InputValidator.birthCitybirthCity field validation
InputValidator.messagemessage (text area in contact form for example) field validation
InputValidator.subjectsubject (object input in contact form for example) field validation
InputValidator.consentconsent (RGPD in contact form for example) field validation
InputValidator.notesnotes (text area) field validation
InputValidator.phoneOnePhone field validation for multiple phone fields in form
InputValidator.phoneTwoPhone field validation for multiple phone fields in form
InputValidator.instagramInstagram field validation
InputValidator.facebookFacebook field validation
InputValidator.websiteWebsite field validation
InputValidator.linkedinLinkedin field validation
InputValidator.fonctionFonction field validation

For My Home Project (but it is possible to use it elsewhere)

NameDescription
InputValidator.questionquestion field validation
InputValidator.questionIdquestion id field validation
InputValidator.categorycategory field validation
InputValidator.introSubThemeintroSubTheme field validation
InputValidator.introThemeintroTheme field validation
InputValidator.subThemesubTheme field validation
InputValidator.themetheme field validation
InputValidator.typetype field validation
InputValidator.answerUnvalidateTextanswerUnvalidateText field validation
InputValidator.answerValidateTextanswerValidateText field validation

Utils

Example for use Utils with this package :

// Require Utils part of this package
const { Utils } = require('@brutdecom/bdc_common')

// or

import { Utils } from '@brutdecom/bdc_common'

// Use utils function
const myConst = Utils.myUtilsFunction()
// return ...
const verifyIfRequestItemsIsString = Utils.validateStringRequestItems({
        value1: 'test1',
        value2: 'test2',
        value3: 12
})
// return false, because one value is not a string

  const verifyIfRequestItemsIsString = Utils.validateStringRequestItems({
        value1: 'test1',
        value2: 'test2'
})
// return true, because all values is string
const orderItems = [
      { _id: '123', productId: '100', productName: 'Item 01', quantity: 10 },
      { _id: '234', productId: '200', productName: 'Item 02', quantity: 6 },
      { _id: '123', productId: '100', productName: 'Item 01', quantity: 2 },
      { _id: '456', productId: '400', productName: 'Item 04', quantity: 23 },
      { _id: '123', productId: '100', productName: 'Item 01', quantity: 1 },
      { _id: '456', productId: '400', productName: 'Item 04', quantity: 9 }
]
const verifyIfRequestItemsIsString = Utils.deleteDuplicateKeysAndMakeSumInObjectArray(orderItems, {
      sumKey: 'quantity',
      idKey: '_id'
})

// Delete items with duplicate "_id", and make sum with quantity keys.
// verifyIfRequestItemsIsString === [
//               {
//                   _id: '123',
//                   productId: '100',
//                   productName: 'Item 01',
//                   quantity: 13
//               },
//               {
//                   _id: '234',
//                   productId: '200',
//                   productName: 'Item 02',
//                   quantity: 6
//               },
//               {
//                   _id: '456',
//                   productId: '400',
//                   productName: 'Item 04',
//                   quantity: 32
//               }
//           ]
const verifyIfExpired = Utils.verifyOrderExpirationTime(date, { 
    unit: 'minutes', // or hours, days etc. See momentjs doc for units
    timeToCompare: 30
  })
  // return true if date is expired, false if not expired
const verifyIfExpired = Utils.normalizeObjectData(date, { 
    unit: 'minutes', // or hours, days etc. See momentjs doc for units
    timeToCompare: 30
  })

// On a un array d'objet en data, que l'on veut normaliser suivant nos normes a nous
const data = [
  { ProductCode: 'SM', ProductID: 48723, DefaultName: 'Test 01', Price: 14, ImageID: 78654, Resa: '', Local: '' },
  { ProductCode: 'SM', ProductID: 48700, DefaultName: 'Test 03', Price: 14, ImageID: 78654, Resa: '', Local: '' },
  { ProductCode: 'SM', ProductID: 48710, DefaultName: 'Test 02', Price: 14, ImageID: 78654, Resa: '', Local: '' }
]

// Premier cas d'utilisation => on veut simplement passer les keys en camelCase et ne garder que certaines keys
// Ce cas contient les params minimum, sinon cela ne fonctionnera pas
const res = await Utils.normalizeObjectData(data, {
  pickedKeys: ['ProductID', 'DefaultName', 'Price', 'ImageID', 'ProductCode'] // les keys que l'on veut garder
})
// console.log(res) => 
// const res = [
//   { productCode: 'SM', productId: 48723, defaultName: 'Test 01', price: 14, imageId: 78654 },
//   { productCode: 'SM', productId: 48700, defaultName: 'Test 03', price: 14, imageId: 78654 },
//   { productCode: 'SM', productId: 48710, defaultName: 'Test 02', price: 14, imageId: 78654 }
// ]

// Second cas d'utilisation => même chose que le premier cas, mais maintenant on veux aussi ajouter des valeurs a chaque object dans l'array
const res = await Utils.normalizeObjectData(data, {
  pickedKeys: ['ProductID', 'DefaultName', 'Price', 'ImageID', 'ProductCode'], // les keys que l'on veut garder
  setValues: [{ key: 'inStock', value: 20 }, { key: 'tax', value: 10 }], // les keys avec les values a ajouter. Attention, cela s'ajoutera a CHAQUE OBJECT DE LA MEME FACON
})
// console.log(res) => 

// const res = [
//   { productCode: 'SM', productId: 48723, defaultName: 'Test 01', price: 14, imageId: 78654, inStock: 20, tax: 10 },
//   { productCode: 'SM', productId: 48700, defaultName: 'Test 03', price: 14, imageId: 78654, inStock: 20, tax: 10 },
//   { productCode: 'SM', productId: 48710, defaultName: 'Test 02', price: 14, imageId: 78654, inStock: 20, tax: 10 }
// ]

// Troisième cas d'utilisation => même chose que les deux premier cas, mais cette fois on replace aussi une key a la place d'une autre, sans changer la valeur
const res = await Utils.normalizeObjectData(data, {
  pickedKeys: ['ProductID', 'DefaultName', 'Price', 'ImageID', 'ProductCode'], // les keys que l'on veut garder
  setValues: [{ key: 'inStock', value: 20 }, { key: 'tax', value: 10 }], // les keys avec les values a ajouter. Attention, cela s'ajoutera a CHAQUE OBJECT DE LA MEME FACON
  replaceKeys: [{ oldKey: 'DefaultName', newKey: 'productName' }]
})
  // console.log(res) => 
  // const res = [
  //   { productCode: 'SM', productId: 48723, productName: 'Test 01', price: 14, imageId: 78654, inStock: 20, tax: 10 },
  //   { productCode: 'SM', productId: 48700, productName: 'Test 03', price: 14, imageId: 78654, inStock: 20, tax: 10 },
  //   { productCode: 'SM', productId: 48710, productName: 'Test 02', price: 14, imageId: 78654, inStock: 20, tax: 10 }
  // ]

Enumerations

:warning: :warning: PLEASE USE NEW ENUM VERSION AND NOT OLD VERSION (IS DEPRECATED) :warning: :warning:

Create Enum

import { Utils } from '@brutdecom/bdc_common'

const CarBrand = {
  RENAULT: 'renault', 
  PEUGEOT_CITROEN: 'peugeot-citroen', 
  FORD: 'ford'
}

export const CarBrandEnum = Utils.createEnum(CarBrand, 'CarBrand')

Methods

CarBrand.getValues() // return ['renault', 'peugeot-citroen', 'ford']
CarBrand.next() // return 'renault'
CarBrand.next('renault') // return 'peugeot-citroen'
CarBrand.next('ford') // return 'renault'
CarBrand.isValid('renault') // return true
CarBrand.isValid('test') // return false
// Update CarBrand with categories for example
const CarBrand = {
  frenchBrand: {
    RENAULT: 'renault', 
    PEUGEOT_CITROEN: 'peugeot-citroen', 
  },
  americanBrand: {
    FORD: 'ford'
  }
}

CarBrand.getCategories() // return ['frenchBrand', 'americanBrand']
// Update CarBrand with categories for example
const CarBrand = {
  frenchBrand: {
    RENAULT: 'renault', 
    PEUGEOT_CITROEN: 'peugeot-citroen', 
  },
  americanBrand: {
    FORD: 'ford'
  }
}

CarBrand.getCategory('frenchBrand') // return ['renault', 'peugeot-citroen']
CarBrand.getIndex('renault') // return 0
CarBrand.getIndex('RENAULT') // return 0
// Update CarBrand with categories for example
const CarBrand = {
    RENAULT: 'renault', 
    PEUGEOT_CITROEN: 'peugeot-citroen',
    FORD: {
      value: 'ford',
      engine: '1.2',
      test: false
    }
  }

CarBrand.getMetaData(CarBrand.FORD, 'engine') // return '1.2'
CarBrand.getMetaData(CarBrand.FORD, 'test') // return false

Examples for use enum functions

Enum example for all examples

    const HabitatThemeQuestionType = [
        {
          value: 'theme-service-book',
          text: `Carnet d'entretien`,
          subTheme: [
              {
                value: 'sub-theme-service-book',
                text: `Carnet d'entretien`
              }
          ]
        },
        {
            value: 'theme-framework',
            text: 'Charpente',
            subTheme: [
                {
                  value: 'sub-theme-wood-state',
                  text: 'Etat du bois'
                },
                {
                    value: 'sub-theme-termites',
                    text: 'Termites'
                }
            ]
          },
          {
            value: 'theme-roof',
            text: 'Toiture',
            subTheme: [
                {
                  value: 'sub-theme-roof-tiles',
                  text: 'Les tuiles'
                },
                {
                  value: 'sub-theme-ridge-and-hip',
                  text: 'Faîtage et arêtier'
                }
            ]
          }
    ]

For use enum functions

Get values of the enum

const { Enum } = require('@brut2com/myhome-common')
const { Utils } = require('@brutdecom/bdc_common')

// or for REACT

import { Enum } from '@brut2com/myhome-common'
import { Utils } from '@brutdecom/bdc_common'


const enumValues = Utils.getEnumValues(Enum.HabitatThemeQuestionType()) // Look enum example Enum.HabitatThemeQuestionType() above for details
// enumValues = ['theme-service-book', 'theme-framework', 'theme-roof']

Get sub type values of the enum

const { Enum } = require('@brut2com/myhome-common')
const { Utils } = require('@brutdecom/bdc_common')

// or for REACT

import { Enum } from '@brut2com/myhome-common')
import { Utils } from '@brutdecom/bdc_common')


const enumSubTypeValues = Utils.getEnumSubTypeValues(Enum.HabitatThemeQuestionType(), 'subTheme') // Look enum example Enum.HabitatThemeQuestionType() above for details
// enumSubTypeValues = ['sub-theme-service-book', 'sub-theme-wood-state', 'sub-theme-termites', 'sub-theme-roof-tiles', 'sub-theme-ridge-and-hip']

Get sub type objects in enum

const { Enum } = require('@brut2com/myhome-common')
const { Utils } = require('@brutdecom/bdc_common')

// or for REACT

import { Enum } from '@brut2com/myhome-common')
import { Utils } from '@brutdecom/bdc_common')


const enumSubType = Utils.getEnumSubTypes(Enum.HabitatThemeQuestionType()) // Look enum example Enum.HabitatThemeQuestionType() above for details
// enumSubType = [
//   {
//     value: 'sub-theme-service-book',
//     text: `Carnet d'entretien`
//   },
//   {
//     value: 'sub-theme-wood-state',
//     text: 'Etat du bois'
//   },
//   {
//     value: 'sub-theme-termites',
//     text: 'Termites'
//   },
//   {
//     value: 'sub-theme-roof-tiles',
//     text: 'Les tuiles'
//   },
//   {
//     value: 'sub-theme-ridge-and-hip',
//     text: 'Faîtage et arêtier'
//    }
//   ]

Get sub type objects in enum

const { Enum } = require('@brut2com/myhome-common')
const { Utils } = require('@brutdecom/bdc_common')

// or for REACT

import { Enum } from '@brut2com/myhome-common')
import { Utils } from '@brutdecom/bdc_common')


const subTypesValuesByParent = Utils.getEnumSubTypeValuesByParent(Enum.HabitatThemeQuestionType(), 'subTheme', 'theme-roof') // Look enum example Enum.HabitatThemeQuestionType() above for details
// subTypesValuesByParent = ['sub-theme-roof-tiles', 'sub-theme-ridge-and-hip']

Get sub type objects in enum

const { Enum } = require('@brut2com/myhome-common')
const { Utils } = require('@brutdecom/bdc_common')

// or for REACT

import { Enum } from '@brut2com/myhome-common')
import { Utils } from '@brutdecom/bdc_common')


const subTypesByParent = Utils.getEnumSubTypeByParent(Enum.HabitatThemeQuestionType(), 'subTheme', 'theme-roof') // Look enum example Enum.HabitatThemeQuestionType() above for details
// subTypesByParent = [
//     {
//       value: 'sub-theme-roof-tiles',
//       text: 'Les tuiles'
//     },
//     {
//       value: 'sub-theme-ridge-and-hip',
//       text: 'Faîtage et arêtier'
//     }
// ])
1.0.73

7 months ago

1.0.72

11 months ago

1.0.71

11 months ago

1.0.66

1 year ago

1.0.65

1 year ago

1.0.64

1 year ago

1.0.69

12 months ago

1.0.68

1 year ago

1.0.67

1 year ago

1.0.70

12 months ago

1.0.63

1 year ago

1.0.62

1 year ago

1.0.61

1 year ago

1.0.60

1 year ago

1.0.55

1 year ago

1.0.54

1 year ago

1.0.53

1 year ago

1.0.52

1 year ago

1.0.59

1 year ago

1.0.58

1 year ago

1.0.57

1 year ago

1.0.56

1 year ago

1.0.48

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.11

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago