Skip to content

Instantly share code, notes, and snippets.

View varijkapil13's full-sized avatar

Varij Kapil varijkapil13

View GitHub Profile
@varijkapil13
varijkapil13 / generateHashValue.java
Created January 27, 2020 10:29
Generate a Hash in Java. Using SHA-1, SHA-256 or MD5
/**
* Generates a SHA-256 hash for a string.
* In case of an exception, it will return the original string value back
*
* @param hashable - the string to be hashed
* @return - the hashed string/ the original string in case of an exception
*/
String generateHashValue(String hashable) {
MessageDigest digest;
try {
@varijkapil13
varijkapil13 / groupBy.javascript.js
Created April 7, 2019 15:44
Grouping elements of an array of objects using a key from the object
/**
* Will return an object which contains all the elements from the `items` array grouped according to the `key` field
* Eg. items = [{name: "ABC", age:2},{name: "DEF", age:2},{name: "GHI", age:2},{name: "JKL", age:3},{name: "MNO", age:3}]
* key = "age"
* result = {2: [{name: "ABC", age:2},{name: "DEF", age:2},{name: "GHI", age:2}], 3: [{name: "JKL", age:3},{name: "MNO", age:3}]}
* @param {Array} items - array of items that needs to be grouped
* @param {String} key - key in the object inside the items array for which the grouping is to be performed
* @returns {*} - resulting object with grouped elements
*/
const groupBy = (items, key) =>
@varijkapil13
varijkapil13 / Moment.get_last_quarters_to_date.js
Created February 21, 2019 17:59
Get Quarter names and dates for the last four quarters from a specific date
export const getLastFourQuarters = (date = moment()) => {
const momentObject = moment(date);
const quarters = {};
for (let i = 0; i <= 3; i++) {
const quarterDate = momentObject.clone().subtract(i * 3, 'month');
const number = quarterDate.quarter();
const year = quarterDate.year();
quarters['Q' + number + ' ' + year] = quarterDate.startOf('quarter');
}
return quarters;
@varijkapil13
varijkapil13 / Moment.get_fiscal_year_to_date.js
Last active February 21, 2019 17:57
Get the start of fiscal year till current date in Moment.js
export const getFiscalYearToDate = () => {
const thisYearMarch = moment().month('March').startOf('month');
if (moment().isAfter(, 'day')){
// new fiscal year has started
return {
startDate : thisYearMarch.clone(),
endDate: moment()
}
} else {
// take the last fiscal year
@varijkapil13
varijkapil13 / iOS Locale Names List
Last active November 24, 2017 11:20
A list of locale names in iOS 11.1
eu Basque
hr_BA Croatian (Bosnia & Herzegovina)
en_CM English (Cameroon)
en_BI English (Burundi)
rw_RW Kinyarwanda (Rwanda)
ast Asturian
en_SZ English (Swaziland)
he_IL Hebrew (Israel)
ar Arabic
uz_Arab Uzbek (Arabic)