Skip to content

Commit

Permalink
Merge pull request #41 from sancodes/refactor_function_property_names…
Browse files Browse the repository at this point in the history
…_util.js

Refactor function or property names #31 - util.js
  • Loading branch information
gomjellie authored Jul 3, 2021
2 parents 49af598 + 5f6cb03 commit bed468a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { Text, View } from 'react-native';
import moment from 'moment';

import { getFormattedDate } from '../utils';
import { formatDate } from '../utils';

import styles from './Header.styles';

Expand Down Expand Up @@ -39,7 +39,7 @@ const Column = ({
return (
<View style={styles.column}>
<Text style={[styles.text, getDayTextStyles(numberOfDays)]}>
{getFormattedDate(column, format)}
{formatDate(column, format)}
</Text>
</View>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/TimeTable/TimeTableView.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Text,
} from 'react-native';
import moment from 'moment';
import { setLocale, addColor, genTimeBlock } from '../utils';
import { setLocale, assignColor, genTimeBlock } from '../utils';
import Events from '../Events/Events';
import Header from '../Header/Header';
import styles from './TimeTableView.styles';
Expand Down Expand Up @@ -55,7 +55,7 @@ export default class TimeTableView extends Component {
onEventPress,
pivotTime,
} = this.props;
const events = addColor(this.props.events);
const events = assignColor(this.props.events);
const { currentMoment } = this.state;
// const dates = this.prepareDates(currentMoment, numberOfDays);
const date = moment(currentMoment);
Expand Down
58 changes: 30 additions & 28 deletions src/components/utils.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import moment from 'moment/min/moment-with-locales.js';

export const getFormattedDate = (date, format) => {
export const formatDate = (date, format) => {
return moment(date).format(format);
};

export const setLocale = (locale) => {
moment.locale(locale);
};

export const getCurrentMonth = (date) => {
return moment(date).format('MMMM Y');
};
//this function should be removed
// export const getCurrentMonth = (date) => {
// return moment(date).format('MMMM Y');
// };

const genDayOfWeek = (DayOfWeekString) => {
const genDateBlock = (dayOW) => {
/*
DayOfWeekString : SUN, MON, TUE, WED, THU, FRI, SAT
type : string
*/
if (typeof DayOfWeekString !== 'string') {
throw new Error(`genDayOfWeek got parameter type: ${typeof DayOfWeekString}, but string expected`);
throw new Error(`genDateBlock got parameter type: ${typeof dayOW}, but string expected`);
}
const str2numberString = {

const dayOWMap = {
'mon': '01',
'tue': '02',
'wed': '03',
Expand All @@ -36,19 +38,19 @@ const genDayOfWeek = (DayOfWeekString) => {
'토': '06',
};

return new Date(`2019-07-${str2numberString[DayOfWeekString.toLowerCase()]}T00:00:00`);
return new Date(`2019-07-${dayOWMap[DayOfWeekString.toLowerCase()]}T00:00:00`);
};

const genTimeBlock = (dayOfWeek, hours = 0, minutes = 0) => {
const date = genDayOfWeek(dayOfWeek);
const genTimeBlock = (dayOW, hours = 0, minutes = 0) => {
const date = genDateBlock(dayOW);
date.setHours(hours);
if (minutes != null) {
date.setMinutes(minutes);
}
return date;
};

const addColor = (events) => {
const assignColor = (events) => {
// add color to item
return events.reduce((acc, item, idx) => {
const sameOne = acc.find((elem) => {
Expand All @@ -62,28 +64,28 @@ const addColor = (events) => {
}, []).length;
acc.push({
...item,
color: sameOne === undefined ? colorGenerator(count) : sameOne.color,
color: sameOne === undefined ? pickColor(count) : sameOne.color,
id: idx,
});
return acc;
}, []);
};

//this function should be removed
// const hashString = (s) => {
// /**
// * String -> Number
// */
// let h, i;
// for (i = 0, h = 0; i < s.length; i++) {
// // eslint-disable-next-line no-bitwise
// h = Math.imul(31, h) + s.charCodeAt(i) | 0;
// }
// return Math.abs(h);
// };

const hashString = (s) => {
/**
* String -> Number
*/
let h, i;
for (i = 0, h = 0; i < s.length; i++) {
// eslint-disable-next-line no-bitwise
h = Math.imul(31, h) + s.charCodeAt(i) | 0;
}
return Math.abs(h);
};

const colorGenerator = (num) => {
const color_list = [
const pickColor = (num) => {
const colorList = [
// apple calendar color
'rgba(246,206,218,1)',
'rgba(250,227,209,1)',
Expand Down Expand Up @@ -122,8 +124,8 @@ const colorGenerator = (num) => {
'rgba(129,199,132,1)',
'rgba(184,0,0,1)',
];
return color_list[num % color_list.length];
return colorList[num % colorList.length];
};

export {genDayOfWeek, genTimeBlock, colorGenerator, addColor};
export { genDateBlock, genTimeBlock, pickColor, assignColor };

4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TimeTableView from './components/TimeTable/TimeTableView';
import { genTimeBlock, addColor } from './components/utils';
import { genTimeBlock, assignColor } from './components/utils';

export { TimeTableView as default };
export { genTimeBlock, addColor };
export { genTimeBlock, assignColor };

0 comments on commit bed468a

Please sign in to comment.