Skip to content

Instantly share code, notes, and snippets.

@yamoo9
Last active February 11, 2020 15:48
Show Gist options
  • Save yamoo9/2e75913037a3b455185bed502d74ec3e to your computer and use it in GitHub Desktop.
Save yamoo9/2e75913037a3b455185bed502d74ec3e to your computer and use it in GitHub Desktop.
helper.js
(function(global, document){
'use strict';
/* -----------------------------------------------------
* DOM 선택 헬퍼 함수 */
function els(selector, context) {
if (typeof selector !== 'string' || selector.trim().length === 0) { return null; }
context = !context ? document : context.nodeType === 1 ? context : el(String(context));
return context.querySelectorAll(selector);
}
function el(selector, context) {
if (typeof selector !== 'string' || selector.trim().length === 0) { return null; }
context = !context ? document : context.nodeType === 1 ? context : el(String(context));
return context.querySelector(selector);
}
/* -----------------------------------------------------
* 날짜,시간 헬퍼 함수 */
function getYear(format) {
return (new Date()).getFullYear() + (format || '');
}
function getMonth(format) {
return ((new Date()).getMonth() + 1) + (format || '');
}
function getDate(format) {
return (new Date()).getDate() + (format || '');
}
function getDay(format) {
var day = (new Date()).getDay();
switch(day) {
case 0: day = '일'; break;
case 1: day = '월'; break;
case 2: day = '화'; break;
case 3: day = '수'; break;
case 4: day = '목'; break;
case 5: day = '금'; break;
case 6: day = '토';
}
return day + (format || '');
}
function getHours(format, ampm) {
var hour = Number((new Date()).getHours());
if (ampm) { ampm = hour < 12 ? 'AM ' : 'PM '; }
else { ampm = ''; }
hour = hour >= 12 ? hour - 12 : hour;
return ampm + (hour < 10 ? '0'+hour : hour) + (format || '');
}
function getMinutes(format) {
return (new Date()).getMinutes() + (format || '');
}
function getSeconds(format) {
return (new Date()).getSeconds() + (format || '');
}
function getMilliseconds(format) {
return (new Date()).getMilliseconds() + (format || '');
}
function getISOString(format) {
return (new Date()).toISOString() + (format || '');
}
/* -----------------------------------------------------
* JavaScript 헬퍼 함수 */
function mixin() {
var mixin_obj = {};
for ( var i=0, l=arguments.length; i<l; ++i) {
var o = arguments[i];
for ( var key in o ) {
var value = o[key];
if (o.hasOwnProperty(key)) { mixin_obj[key] = value; }
}
}
return mixin_obj;
};
function defineProp(value, descripter) {
descripter = mixin({
configurable: false,
writable: false,
enumerable: true,
}, (descripter || {}));
descripter.value = value;
console.log(descripter);
return descripter;
}
/* -----------------------------------------------------
* y9 네임스페이스 객체 생성 및 메서드 설정 */
Object.defineProperty(global, 'y9', {
value: {}
});
Object.defineProperties(global.y9, {
// DOM
els : defineProp(els),
el : defineProp(el),
// Date
getYear : defineProp(getYear),
getMonth : defineProp(getMonth),
getDate : defineProp(getDate),
getDay : defineProp(getDay),
getHours : defineProp(getHours),
getMinutes : defineProp(getMinutes),
getSeconds : defineProp(getSeconds),
getMilliseconds : defineProp(getMilliseconds),
getISOString : defineProp(getISOString),
// JavaScript
mixin : defineProp(mixin),
});
// Object.preventExtensions(global.y9);
// Object.seal(global.y9);
// Object.freeze(global.y9);
})(window, window.document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment