1.0.7 • Published 4 years ago

@baltimorecounty/javascript-utilities v1.0.7

Weekly downloads
32
License
ISC
Repository
github
Last release
4 years ago

Baltimore County Javascript Utilities

A group of javascript utilities that have been found to be useful across different projects at Baltimore County.

Config Utilities

The config utilities help us share configuration values based on the cms environment they are in. Our current cms makes it extremely difficult to manage our config values in our apps.

import

import { Config } from "@baltimorecounty/javascript-utilities";
import {
  setConfig,
  getValue,
  config
} = Config;

setConfig

The constructor takes one parameter, "values", which is an object which contains configuration for the following environments: local, development, staging, production;

This utility will determine which environment is being used based on the contents of the browser URL per the following:

  • local - any url that contains localhost
  • development - any url with dev subdomain - Example: dev.baltimorecountymd.gov
  • staging - any url with staging subdomain - Example: staging.baltimorecountymd.gov
  • production - any url with www. or no subdomain but not localhost - Example: www.baltimorecountymd.gov

Usage

import { Config } from "@baltimorecounty/javascript-utilities";
const { setConfig } = Config;

const configValues = {
  local: {
    apiRoot: "http://localhost:1919/api",
    title: "Local - My Awesome App"
  },
  development: {
    apiRoot: "http://testservices.baltimorecountymd.gov/api",
    title: "Development - My Awesome App"
  },
  staging: {
    apiRoot: "http://stagingservices.baltimorecountymd.gov/api",
    title: "Staging - My Awesome App"
  },
  production: {
    apiRoot: "http://services.baltimorecountymd.gov/api",
    title: "My Awesome App"
  }
};

setConfig(configValues); // Sets the config for use with either GetValue or Config

Note: You will want to include this either at the beginning of your script inclusions or your app.js in a React app.

getValue

This function takes a parameter of the key of the configuration value you wish to return, e.g. "title".

You will get an error in the console describing what went wrong if the config hasn't been set, you are in an environment that doesn't exist, or you pass in a key that doesn't exist.

Usage

import { Config } from "@baltimorecounty/javascript-utilities";
const { getValue } = Config;
const apiRoot = getValue("title"); // for local environments returns "Local - My Awesome App" if used with the config object from the above example

config

Returns the entire config object

Usage

import { Config } from "@baltimorecounty/javascript-utilities";
const { config } = Config;
const apiRoot = console.log(config);
// The console log will output the below representation of the config object:
{
	local: {
		apiRoot: 'http://localhost:1919/api',
		title: 'Local - My Awesome App'
	},
	development: {
		apiRoot: 'http://testservices.baltimorecountymd.gov/api',
		title: 'Development - My Awesome App'
	},
	staging: {
		apiRoot: 'http://stagingservices.baltimorecountymd.gov/api',
		title: 'Staging - My Awesome App'
	},
	production: {
		apiRoot: 'http://services.baltimorecountymd.gov/api',
		title: 'My Awesome App'
	}
}

Url Utilities

The config utilities help us deal with the url when we need to

GetParameterByName

This function takes a parameter of the query parameter name of the configuration value you wish to return, e.g. "name". This function uses the current url, specifically the location.search property to work it's magic.

Usage

Example Url: http://abc.com?name=bob&age=20

To get Bob's age you would do the following

import { Urls } from "@baltimorecounty/javascript-utilities";
const { GetParameterByName } = Urls;

const bobsAge = GetParameterByName("age"); // returns "20"
1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago