Skip to content

Instantly share code, notes, and snippets.

@dsk52
Last active August 27, 2018 22:35
Show Gist options
  • Save dsk52/aa8a2bc30a097ba60309fbf94acc0cda to your computer and use it in GitHub Desktop.
Save dsk52/aa8a2bc30a097ba60309fbf94acc0cda to your computer and use it in GitHub Desktop.
utility set
const DateUtil = {
thisYear: () => {
return new Date().getFullYear().toString();
},
thisMonth: () => {
const month = new Date().getMonth() + 1;
if (month < 10) {
return "0" + month.toString();
} else {
return month.toString();
}
},
allMonths: () => {
return Array.from(Array(12).keys()).map(i => {
const month = i + 1;
let value = "";
if (month < 10) {
value = "0" + month.toString();
} else {
value = month.toString();
}
return {
label: value,
value: value
};
});
},
allWeeks: () => {
return Array.from(Array(52).keys()).map(i => {
const week = i + 1;
let value = week.toString();
if (week <= 9) {
value = "0" + value;
}
return {
label: week.toString() + "週",
value: value
};
});
},
yearOptionList: () => {
const options = [];
const year = new Date().getFullYear();
for (let index = -1; index < 3; index++) {
let value = year + index;
options.push({
label: `${value}年`,
value: value
});
}
return options;
},
zeroPadding: date => {
if (date < 10) {
return "0" + date.toString();
} else {
return date.toString();
}
}
};
export { DateUtil };
import config from "@/config";
const ImagePath = {
create: name => {
return `${config.IMAGE_PATH}/${name}`;
}
};
export { ImagePath };
const Number = {
separateNum: num => {
if (num !== null) {
return String(num).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
} else {
return 0;
}
},
percentNum: num => {
if (num !== null) {
return parseFloat(num).toFixed(1);
} else {
return "0.0";
}
},
max: array => {
return Math.max(...array);
},
calcCompressionRatio: (amount, sales, price) => {
return Math.round((amount / (sales * price) * 100) * 10) / 10.0;
}
};
export { Number };
const String = {
getUniqueStr: () => {
var strong = 1000;
return (
new Date().getTime().toString(16) +
Math.floor(strong * Math.random()).toString(16)
);
},
toUpperFirstLetter: str => {
if (str != null) {
return str.charAt(0).toUpperCase() + str.substring(1).toLowerCase();
} else {
return null;
}
}
};
export { String };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment