add util/dom

This commit is contained in:
Andrew
2015-04-01 09:33:48 -06:00
parent a337aca26b
commit e79973fe5d
15 changed files with 305 additions and 165 deletions

33
src/util/dom.js Normal file
View File

@@ -0,0 +1,33 @@
const nativeRaf= window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame
const nativeCancelRaf = window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame
export const raf = nativeRaf || function(callback) {
return window.setTimeout(callback, 16.6667)
}
export const rafCancel = nativeRaf ? nativeCancelRaf : function(id) {
return window.cancelTimeout(id)
}
export function rafPromise() {
return new Promise(resolve => raf(resolve))
}
// We only need to test for webkit in our supported browsers. Webkit is the only browser still
// using prefixes.
// Code adapted from angular-animate.js
export let css = {}
if (window.ontransitionend === undefined && window.onwebkittransitionend !== undefined) {
css.prefix = 'webkit'
css.transition = 'webkitTransition'
css.transitionEnd = 'webkitTransitionEnd transitionend'
} else {
css.prefix = ''
css.transition = 'transition'
css.transitionEnd = 'tranistionend'
}

View File

@@ -0,0 +1,52 @@
import * as util from 'ionic2/util';
export function run() {
describe('extend', function() {
it('should extend simple', () => {
var obj = { a: '0', c: '0' };
expect( util.extend(obj, { a: '1', b: '2' }) ).toBe(obj);
expect(obj).toEqual({ a: '1', b: '2', c: '0' });
});
it('should extend complex', () => {
expect(util.extend(
{ a: '0', b: '0' },
{ b: '1', c: '1' },
{ c: '2', d: '2' }
)).toEqual({
a: '0',
b: '1',
c: '2',
d: '2'
});
});
});
describe('defaults', function() {
it('should simple defaults', () => {
var obj = { a: '1' };
expect(util.defaults(obj, { a: '2', b: '2' })).toBe(obj);
expect(obj).toEqual({
a: '1', b: '2'
});
});
it('should complex defaults', () => {
expect(util.defaults(
{ a: '0', b: '0' },
{ b: '1', c: '1', e: '1' },
{ c: '2', d: '2' }
)).toEqual({
a: '0',
b: '0',
c: '2',
d: '2',
e: '1'
});
});
});
}

71
src/util/util.js Normal file
View File

@@ -0,0 +1,71 @@
export function noop() {}
export function clamp(min, n, max) {
return Math.max(min, Math.min(n, max));
}
export function extend(dest) {
for (var i = 1, ii = arguments.length; i < ii; i++) {
var source = arguments[i] || {};
for (var key in source) {
if (source.hasOwnProperty(key)) {
dest[key] = source[key];
}
}
}
return dest;
}
export function defaults(dest) {
for (let i = arguments.length - 1; i >= 1; i--) {
let source = arguments[i] || {};
for (let key in source) {
if (source.hasOwnProperty(key) && !dest.hasOwnProperty(key)) {
dest[key] = source[key];
}
}
}
return dest;
}
export const isString = val => typeof val === 'string'
export const isFunction = val => typeof val === 'function'
export const isDefined = val => typeof val === 'undefined'
export const isObject = val => typeof val === 'object'
export const isArray = Array.isArray
export function pascalCaseToDashCase(str = '') {
return str.charAt(0).toLowerCase() + str.substring(1).replace(/[A-Z]/g, match => {
return '-' + match.toLowerCase()
})
}
export class Log {
static log(...args) {
console.log.apply(console, args)
}
static info(...args) {
console.info.apply(console, args)
}
static warn(...args) {
console.warn.apply(console, args)
}
static error(...args) {
console.error.apply(console, args)
}
}
export function readQueryParams() {
var queryParams = {}
const startIndex = window.location.href.indexOf('?')
if (startIndex !== -1) {
const queries = window.location.href.slice(startIndex + 1).split('&')
if (queries.length) {
queries.forEach((param) => {
var split = param.split('=')
queryParams[split[0]] = split[1]
})
}
}
return queryParams
}