mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
73
lib/core/utils/file-utils.ts
Normal file
73
lib/core/utils/file-utils.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export interface FileInfo {
|
||||
entry?: WebKitFileEntry;
|
||||
file?: File;
|
||||
relativeFolder?: string;
|
||||
}
|
||||
|
||||
export class FileUtils {
|
||||
|
||||
static flattern(folder: any): Promise<FileInfo[]> {
|
||||
let reader = folder.createReader();
|
||||
let files: FileInfo[] = [];
|
||||
return new Promise(resolve => {
|
||||
let iterations = [];
|
||||
(function traverse() {
|
||||
reader.readEntries((entries) => {
|
||||
if (!entries.length) {
|
||||
Promise.all(iterations).then(result => resolve(files));
|
||||
} else {
|
||||
iterations.push(Promise.all(entries.map(entry => {
|
||||
if (entry.isFile) {
|
||||
return new Promise(resolveFile => {
|
||||
entry.file(function (f: File) {
|
||||
files.push({
|
||||
entry: entry,
|
||||
file: f,
|
||||
relativeFolder: entry.fullPath.replace(/\/[^\/]*$/, '')
|
||||
});
|
||||
resolveFile();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return FileUtils.flattern(entry).then(result => {
|
||||
files.push(...result);
|
||||
});
|
||||
}
|
||||
})));
|
||||
// Try calling traverse() again for the same dir, according to spec
|
||||
traverse();
|
||||
}
|
||||
});
|
||||
})();
|
||||
});
|
||||
}
|
||||
|
||||
static toFileArray(fileList: FileList): File[] {
|
||||
let result = [];
|
||||
|
||||
if (fileList && fileList.length > 0) {
|
||||
for (let i = 0; i < fileList.length; i++) {
|
||||
result.push(fileList[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
18
lib/core/utils/index.ts
Normal file
18
lib/core/utils/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export * from './public-api';
|
220
lib/core/utils/momentDateAdapter.ts
Normal file
220
lib/core/utils/momentDateAdapter.ts
Normal file
@@ -0,0 +1,220 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { DateAdapter, MatDateFormats } from '@angular/material';
|
||||
import { isMoment, Moment } from 'moment';
|
||||
import * as moment from 'moment';
|
||||
|
||||
export const MOMENT_DATE_FORMATS: MatDateFormats = {
|
||||
parse: {
|
||||
dateInput: 'DD/MM/YYYY'
|
||||
},
|
||||
display: {
|
||||
dateInput: 'DD/MM/YYYY',
|
||||
monthYearLabel: 'MMMM Y',
|
||||
dateA11yLabel: 'LL',
|
||||
monthYearA11yLabel: 'MMMM Y'
|
||||
}
|
||||
};
|
||||
|
||||
const dateNames: string[] = [];
|
||||
for (let date = 1; date <= 31; date++) {
|
||||
dateNames.push(String(date));
|
||||
}
|
||||
|
||||
export class MomentDateAdapter extends DateAdapter<Moment> {
|
||||
|
||||
private localeData: any = moment.localeData();
|
||||
|
||||
overrideDisplyaFormat: string;
|
||||
|
||||
getYear(date: Moment): number {
|
||||
return date.year();
|
||||
}
|
||||
|
||||
getMonth(date: Moment): number {
|
||||
return date.month();
|
||||
}
|
||||
|
||||
getDate(date: Moment): number {
|
||||
return date.date();
|
||||
}
|
||||
|
||||
getDayOfWeek(date: Moment): number {
|
||||
return date.day();
|
||||
}
|
||||
|
||||
getMonthNames(style: 'long' | 'short' | 'narrow'): string[] {
|
||||
switch (style) {
|
||||
case 'long':
|
||||
return this.localeData.months();
|
||||
case 'short':
|
||||
return this.localeData.monthsShort();
|
||||
case 'narrow':
|
||||
return this.localeData.monthsShort().map(month => month[0]);
|
||||
default :
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
getDateNames(): string[] {
|
||||
return dateNames;
|
||||
}
|
||||
|
||||
getDayOfWeekNames(style: 'long' | 'short' | 'narrow'): string[] {
|
||||
switch (style) {
|
||||
case 'long':
|
||||
return this.localeData.weekdays();
|
||||
case 'short':
|
||||
return this.localeData.weekdaysShort();
|
||||
case 'narrow':
|
||||
return this.localeData.weekdaysShort();
|
||||
default :
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
getYearName(date: Moment): string {
|
||||
return String(date.year());
|
||||
}
|
||||
|
||||
getFirstDayOfWeek(): number {
|
||||
return this.localeData.firstDayOfWeek();
|
||||
}
|
||||
|
||||
getNumDaysInMonth(date: Moment): number {
|
||||
return date.daysInMonth();
|
||||
}
|
||||
|
||||
clone(date: Moment): Moment {
|
||||
return date.clone();
|
||||
}
|
||||
|
||||
createDate(year: number, month: number, date: number): Moment {
|
||||
return moment([year, month, date]);
|
||||
}
|
||||
|
||||
today(): Moment {
|
||||
return moment();
|
||||
}
|
||||
|
||||
parse(value: any, parseFormat: any): Moment {
|
||||
let m = moment(value, parseFormat, true);
|
||||
if (!m.isValid()) {
|
||||
m = moment(value, this.overrideDisplyaFormat);
|
||||
}
|
||||
if (m.isValid()) {
|
||||
// if user omits year, it defaults to 2001, so check for that issue.
|
||||
if (m.year() === 2001 && value.indexOf('2001') === -1) {
|
||||
// if 2001 not actually in the value string, change to current year
|
||||
const currentYear = new Date().getFullYear();
|
||||
m.set('year', currentYear);
|
||||
// if date is in the future, set previous year
|
||||
if (m.isAfter(moment())) {
|
||||
m.set('year', currentYear - 1);
|
||||
}
|
||||
}
|
||||
return m;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
format(date: Moment, displayFormat: any): string {
|
||||
|
||||
displayFormat = this.overrideDisplyaFormat ? this.overrideDisplyaFormat : displayFormat;
|
||||
|
||||
if (date && date.format) {
|
||||
return date.format(displayFormat);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
addCalendarYears(date: Moment, years: number): Moment {
|
||||
return date.clone().add(years, 'y');
|
||||
}
|
||||
|
||||
addCalendarMonths(date: Moment, months: number): Moment {
|
||||
return date.clone().add(months, 'M');
|
||||
}
|
||||
|
||||
addCalendarDays(date: Moment, days: number): Moment {
|
||||
return date.clone().add(days, 'd');
|
||||
}
|
||||
|
||||
getISODateString(date: Moment): string {
|
||||
return date.toISOString();
|
||||
}
|
||||
|
||||
setLocale(locale: any): void {
|
||||
this.localeData = moment.localeData(locale);
|
||||
}
|
||||
|
||||
compareDate(first: Moment, second: Moment): number {
|
||||
return first.diff(second, 'seconds', true);
|
||||
}
|
||||
|
||||
sameDate(first: any | Moment, second: any | Moment): boolean {
|
||||
if (first == null) {
|
||||
// same if both null
|
||||
return second == null;
|
||||
} else if (isMoment(first)) {
|
||||
return first.isSame(second);
|
||||
} else {
|
||||
const isSame = super.sameDate(first, second);
|
||||
return isSame;
|
||||
}
|
||||
}
|
||||
|
||||
clampDate(date: Moment, min?: any | Moment, max?: any | Moment): Moment {
|
||||
if (min && date.isBefore(min)) {
|
||||
return min;
|
||||
} else if (max && date.isAfter(max)) {
|
||||
return max;
|
||||
} else {
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
isDateInstance(date: any) {
|
||||
let isValidDateInstance = false;
|
||||
|
||||
if (date) {
|
||||
isValidDateInstance = date._isAMomentObject;
|
||||
}
|
||||
|
||||
return isValidDateInstance;
|
||||
}
|
||||
|
||||
isValid(date: Moment): boolean {
|
||||
return date.isValid();
|
||||
}
|
||||
|
||||
toIso8601(date: Moment): string {
|
||||
return this.clone(date).format();
|
||||
}
|
||||
|
||||
fromIso8601(iso8601String: string): Moment | null {
|
||||
let d = moment(iso8601String, moment.ISO_8601).locale(this.locale);
|
||||
return this.isValid(d) ? d : null;
|
||||
}
|
||||
|
||||
invalid(): Moment {
|
||||
return moment.invalid();
|
||||
}
|
||||
}
|
133
lib/core/utils/object-utils.spec.ts
Normal file
133
lib/core/utils/object-utils.spec.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ObjectUtils } from './object-utils';
|
||||
|
||||
describe('ObjectUtils', () => {
|
||||
|
||||
it('should get top level property value', () => {
|
||||
let obj = {
|
||||
id: 1
|
||||
};
|
||||
expect(ObjectUtils.getValue(obj, 'id')).toBe(1);
|
||||
});
|
||||
|
||||
it('should not get top level property value', () => {
|
||||
let obj = {};
|
||||
expect(ObjectUtils.getValue(obj, 'missing')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should get nested property value', () => {
|
||||
let obj = {
|
||||
name: {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe'
|
||||
}
|
||||
};
|
||||
|
||||
expect(ObjectUtils.getValue(obj, 'name.lastName')).toBe('Doe');
|
||||
});
|
||||
|
||||
it('should not get nested property value', () => {
|
||||
let obj = {};
|
||||
expect(ObjectUtils.getValue(obj, 'some.missing.property')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined when getting value for missing target', () => {
|
||||
expect(ObjectUtils.getValue(null, 'id')).toBeUndefined();
|
||||
});
|
||||
|
||||
describe('merge', () => {
|
||||
it('should merge top level properties', () => {
|
||||
const obj1 = {
|
||||
prop1: 'value1'
|
||||
};
|
||||
|
||||
const obj2 = {
|
||||
prop2: 'value2'
|
||||
};
|
||||
|
||||
const result = ObjectUtils.merge(obj1, obj2);
|
||||
expect(result).toEqual({
|
||||
prop1: 'value1',
|
||||
prop2: 'value2'
|
||||
});
|
||||
});
|
||||
|
||||
it('should merge object properties', () => {
|
||||
const obj1 = {
|
||||
child: {
|
||||
prop1: 1
|
||||
},
|
||||
prop: 1
|
||||
};
|
||||
const obj2 = {
|
||||
child: {
|
||||
prop1: 2,
|
||||
prop2: 3
|
||||
}
|
||||
};
|
||||
|
||||
const result = ObjectUtils.merge(obj1, obj2);
|
||||
expect(result).toEqual({
|
||||
child: {
|
||||
prop1: 2,
|
||||
prop2: 3
|
||||
},
|
||||
prop: 1
|
||||
});
|
||||
});
|
||||
|
||||
it('should merge arrays', () => {
|
||||
const obj1 = {
|
||||
arr: [1, 2, 3]
|
||||
};
|
||||
const obj2 = {
|
||||
arr: [4, 5, 6]
|
||||
};
|
||||
|
||||
const result = ObjectUtils.merge(obj1, obj2);
|
||||
expect(result).toEqual({
|
||||
arr: [1, 2, 3, 4, 5, 6]
|
||||
});
|
||||
});
|
||||
|
||||
it('shoud overwrite only single property in the object', () => {
|
||||
const obj1 = {
|
||||
child: {
|
||||
prop1: 1,
|
||||
prop2: 2,
|
||||
prop3: 3
|
||||
}
|
||||
};
|
||||
const obj2 = {
|
||||
child: {
|
||||
prop3: 0
|
||||
}
|
||||
};
|
||||
|
||||
const result = ObjectUtils.merge(obj1, obj2);
|
||||
expect(result).toEqual({
|
||||
child: {
|
||||
prop1: 1,
|
||||
prop2: 2,
|
||||
prop3: 0
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
68
lib/core/utils/object-utils.ts
Normal file
68
lib/core/utils/object-utils.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export class ObjectUtils {
|
||||
/**
|
||||
* Gets a value from an object by composed key
|
||||
* ObjectUtils.getValue({ item: { nodeType: 'cm:folder' }}, 'item.nodeType') ==> 'cm:folder'
|
||||
* @param target
|
||||
* @param key
|
||||
* @returns {string}
|
||||
*/
|
||||
static getValue(target: any, key: string): any {
|
||||
|
||||
if (!target) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let keys = key.split('.');
|
||||
key = '';
|
||||
|
||||
do {
|
||||
key += keys.shift();
|
||||
let value = target[key];
|
||||
if (value !== undefined && (typeof value === 'object' || !keys.length)) {
|
||||
target = value;
|
||||
key = '';
|
||||
} else if (!keys.length) {
|
||||
target = undefined;
|
||||
} else {
|
||||
key += '.';
|
||||
}
|
||||
} while (keys.length);
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
static merge(...objects): any {
|
||||
let result = {};
|
||||
|
||||
objects.forEach(source => {
|
||||
Object.keys(source).forEach(prop => {
|
||||
if (prop in result && Array.isArray(result[prop])) {
|
||||
result[prop] = result[prop].concat(source[prop]);
|
||||
} else if (prop in result && typeof result[prop] === 'object') {
|
||||
result[prop] = ObjectUtils.merge(result[prop], source[prop]);
|
||||
} else {
|
||||
result[prop] = source[prop];
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
20
lib/core/utils/public-api.ts
Normal file
20
lib/core/utils/public-api.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export * from './object-utils';
|
||||
export * from './file-utils';
|
||||
export * from './momentDateAdapter';
|
Reference in New Issue
Block a user