feature(common) convert to ESM

This commit is contained in:
coderaiser 2020-12-22 13:29:19 +02:00
parent d6d587af66
commit 990fbde90f
12 changed files with 49 additions and 86 deletions

View file

@ -1,6 +1,4 @@
'use strict';
module.exports = (str) => {
export default (str) => {
if (typeof btoa === 'function')
return btoa(str);

View file

@ -1,8 +1,6 @@
'use strict';
const success = (f) => (data) => f(null, data);
module.exports = (promise) => {
export default (promise) => {
return (...a) => {
const fn = a.pop();

View file

@ -1,7 +1,5 @@
'use strict';
const test = require('supertape');
const callbackify = require('./callbackify');
import test from 'supertape';
import callbackify from './callbackify.js';
test('cloudcmd: common: callbackify: error', (t) => {
const promise = async () => {

View file

@ -1,30 +1,22 @@
'use strict';
const rendy = require('rendy');
const currify = require('currify');
const store = require('fullstore');
const {encode} = require('./entity');
const btoa = require('./btoa');
const getHeaderField = currify(_getHeaderField);
import rendy from 'rendy';
import currify from 'currify';
import store from 'fullstore';
import {encode} from './entity.js';
import btoa from './btoa.js';
/* КОНСТАНТЫ (общие для клиента и сервера)*/
/* название программы */
const NAME = 'Cloud Commander';
const FS = '/fs';
export const FS = '/fs';
const Path = store();
Path('/');
module.exports.FS = FS;
module.exports.apiURL = '/api/v1';
module.exports.MAX_FILE_SIZE = 500 * 1024;
module.exports.getHeaderField = getHeaderField;
module.exports.getPathLink = getPathLink;
module.exports.getDotDot = getDotDot;
export const apiURL = '/api/v1';
export const MAX_FILE_SIZE = 500 * 1024;
module.exports.formatMsg = (msg, name, status) => {
export const formatMsg = (msg, name, status) => {
status = status || 'ok';
name = name || '';
@ -38,7 +30,7 @@ module.exports.formatMsg = (msg, name, status) => {
* Функция возвращает заголовок веб страницы
* @path
*/
module.exports.getTitle = (options) => {
export const getTitle = (options) => {
options = options || {};
const {
@ -60,6 +52,10 @@ module.exports.getTitle = (options) => {
* возвращаеться массив каталогов
* @param url - адрес каталога
*/
export /** Функция получает адреса каждого каталога в пути
* возвращаеться массив каталогов
* @param url - адрес каталога
*/
function getPathLink(url, prefix, template) {
if (!url)
throw Error('url could not be empty!');
@ -108,7 +104,7 @@ const getDataName = (name) => {
* @param params - информация о файлах
*
*/
module.exports.buildFromJSON = (params) => {
export const buildFromJSON = (params) => {
const {
prefix,
template,
@ -248,7 +244,7 @@ function getAttribute(type) {
return 'target="_blank" ';
}
module.exports._getSize = getSize;
export const _getSize = getSize;
function getSize(file) {
const {
size,
@ -264,7 +260,7 @@ function getSize(file) {
return size;
}
function _getHeaderField(sort, order, name) {
export const getHeaderField = currify((sort, order, name) => {
const arrow = order === 'asc' ? '↑' : '↓';
if (sort !== name)
@ -274,9 +270,9 @@ function _getHeaderField(sort, order, name) {
return name;
return `${name}${arrow}`;
}
});
function getDotDot(path) {
export function getDotDot(path) {
// убираем последний слеш и каталог в котором мы сейчас находимся
const lastSlash = path.substr(path, path.lastIndexOf('/'));
const dotDot = lastSlash.substr(lastSlash, lastSlash.lastIndexOf('/'));

View file

@ -1,7 +1,5 @@
'use strict';
const test = require('supertape');
const {_getSize} = require('./cloudfunc');
import test from 'supertape';
import {_getSize} from './cloudfunc.js';
test('cloudfunc: getSize: dir', (t) => {
const type = 'directory';

View file

@ -1,8 +1,6 @@
'use strict';
import shortdate from 'shortdate';
const shortdate = require('shortdate');
module.exports = (date) => {
export default (date) => {
date = date || new Date();
check(date);

View file

@ -1,9 +1,7 @@
'use strict';
import test from 'supertape';
import tryCatch from 'try-catch';
const test = require('supertape');
const tryCatch = require('try-catch');
const datetime = require('./datetime');
import datetime from './datetime.js';
test('common: datetime', (t) => {
const dateStr = 'Fri, 17 Aug 2018 10:56:48';

View file

@ -1,5 +1,3 @@
'use strict';
const Entities = {
' ': ' ',
'&lt;': '<',
@ -9,7 +7,7 @@ const Entities = {
const keys = Object.keys(Entities);
module.exports.encode = (str) => {
export const encode = (str) => {
for (const code of keys) {
const char = Entities[code];
const reg = RegExp(char, 'g');
@ -20,7 +18,7 @@ module.exports.encode = (str) => {
return str;
};
module.exports.decode = (str) => {
export const decode = (str) => {
for (const code of keys) {
const char = Entities[code];
const reg = RegExp(code, 'g');

View file

@ -1,9 +1,7 @@
'use strict';
const tryToCatch = require('try-to-catch');
import tryToCatch from 'try-to-catch';
const all = Promise.all.bind(Promise);
module.exports = async (a) => {
export default async (a) => {
const [e, result = []] = await tryToCatch(all, a);
return [

View file

@ -1,7 +1,5 @@
'use strict';
const test = require('supertape');
const tryToPromiseAll = require('./try-to-promise-all');
import test from 'supertape';
import tryToPromiseAll from './try-to-promise-all.js';
const resolve = Promise.resolve.bind(Promise);
const reject = Promise.reject.bind(Promise);

View file

@ -1,8 +1,10 @@
'use strict';
import exec from 'execon';
const exec = require('execon');
export {
exec,
};
module.exports.escapeRegExp = (str) => {
export const escapeRegExp = (str) => {
const isStr = typeof str === 'string';
if (isStr)
@ -14,7 +16,7 @@ module.exports.escapeRegExp = (str) => {
/**
* get regexp from wild card
*/
module.exports.getRegExp = (wildcard) => {
export const getRegExp = (wildcard) => {
const escaped = '^' + wildcard // search from start of line
.replace(/\./g, '\\.')
.replace(/\*/g, '.*')
@ -23,15 +25,13 @@ module.exports.getRegExp = (wildcard) => {
return RegExp(escaped);
};
module.exports.exec = exec;
/**
* function gets file extension
*
* @param name
* @return ext
*/
module.exports.getExt = (name) => {
export const getExt = (name) => {
const isStr = typeof name === 'string';
if (!isStr)
@ -51,7 +51,7 @@ module.exports.getExt = (name) => {
* @param array
* @param name
*/
module.exports.findObjByNameInArr = (array, name) => {
export const findObjByNameInArr = (array, name) => {
let ret;
if (!Array.isArray(array))
@ -89,7 +89,7 @@ module.exports.findObjByNameInArr = (array, name) => {
* start timer
* @param name
*/
module.exports.time = (name) => {
export const time = (name) => {
exec.ifExist(console, 'time', [name]);
};
@ -97,7 +97,7 @@ module.exports.time = (name) => {
* stop timer
* @param name
*/
module.exports.timeEnd = (name) => {
export const timeEnd = (name) => {
exec.ifExist(console, 'timeEnd', [name]);
};

View file

@ -1,9 +1,6 @@
'use strict';
const test = require('supertape');
const {reRequire} = require('mock-require');
const tryCatch = require('try-catch');
const Util = require('./util');
import test from 'supertape';
import tryCatch from 'try-catch';
import * as Util from './util.js';
const {
findObjByNameInArr,
@ -122,15 +119,3 @@ test('util: escapeRegExp', (t) => {
t.end();
});
test('util: scope', (t) => {
global.window = {};
reRequire('./util');
t.pass('should set window in scope');
delete global.window;
t.end();
});