Paulund

In this tutorial we’re going to change a string of words to be camel-cased.

You can do this by using the below regex to select all the matched parts of the string and then on the first index you lowercase the word and on the next indexes you can make uppercase.

export const camel = (text: string): string => {
return text.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // if space
return index === 0 ? match.toLowerCase() : match.toUpperCase();
});
}

This will allow you to take the following scenarios and camel-case the response.

import {camel} from "./camel";test('it will camelcase text', () => {
expect(camel('CamelCaseClassName')).toBe('camelCaseClassName')
expect(camel('Camel Case Class Name')).toBe('camelCaseClassName')
expect(camel('Camel CaseClassName')).toBe('camelCaseClassName')
expect(camel('camel case class name')).toBe('camelCaseClassName')
expect(camel('CamelCase ClassName')).toBe('camelCaseClassName')
expect(camel('CamelCaseClass Name')).toBe('camelCaseClassName')
});

Camel Function

Originally published at https://paulund.co.uk.

--

--

Here is a quick code snippet you can use in your projects to check if an array is empty.

const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0;isNotEmpty([1, 2, 3]); // Result: trueisNotEmpty([]); // Result: false

isNotEmpty

If you like you can create the opposite helper function to check if the array is empty.

const isEmpty = arr => Array.isArray(arr) && arr.length === 0;isEmpty([1, 2, 3]); // Result: falseisEmpty([]); // Result: true

isEmpty

--

--

Here is a quick code snippet I use to capitalize the first letter in a string.

// Capitalize the first letter of a string
const capitalize = (text) => `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
console.log(capitalize("here is some text"));
// Here is some text

This will take the first letter of the string and uppercase the letter. Then it will concatenate the rest of the string without the first letter.

ucfirst function

Originally published at https://paulund.co.uk.

--

--

When working with APIs in your Javascript application it can be useful to be able to debug the requests and responses you’re using to interact with the API.

Axios comes with a feature called interceptors that allow you to run some code before a request and after a response. This is a good place to add some code that runs on every request, you can put your debug messages in here.

axios.interceptors.request.use(request => {
console.log('Starting Request', JSON.stringify(request, null, 2))
return request
})
axios.interceptors.response.use(response => {
console.log('Response:', JSON.stringify(response, null, 2))
return response
})

Now when you use axios to make requests to apis you’ll get debug messages for each request and response.

Originally published at https://paulund.co.uk.

--

--