Example #1
0
router.post('/save', async (req: any, res) => {
  if (req.query.type === 'file') {
    if (req.query.filename === '') {
      res.send({});
      return;
    }

    if (req.query.filename === '.json') {
      res.send({});
      return;
    }

    await fileManager.saveFile(
      req.user.id,
      `${trimStart(req.query.dirname, '/')}/${req.query.filename}`,
      req.body.content,
    );
  } else {
    await fileManager.createDir(
      req.user.id,
      `${trimStart(req.query.dirname, '/')}/${req.query.filename}`,
    );
  }

  res.send({});
});
Example #2
0
        map(lines => {
            const codeElement = singleFileDOMFunctions.getCodeElementFromLineNumber(
                codeView,
                position.line,
                position.part
            )
            if (!codeElement) {
                throw new Error('(adjustPosition) could not find code element for line provided')
            }

            const actualLine = lines[position.line - 1]
            const documentLine = codeElement.textContent || ''

            const actualLeadingWhiteSpace = actualLine.length - trimStart(actualLine).length
            const documentLeadingWhiteSpace = documentLine.length - trimStart(documentLine).length

            const modifier = direction === AdjustmentDirection.ActualToCodeView ? -1 : 1
            const delta = Math.abs(actualLeadingWhiteSpace - documentLeadingWhiteSpace) * modifier

            return {
                line: position.line,
                character: position.character + delta,
            }
        })
Example #3
0
import {trim, trimStart, trimEnd, pad, padStart, padEnd} from "lodash";

const trimStr: string = "     trim        ";
console.log("trimStart()");
console.log("*" + trimStart(trimStr) + "*");
console.log("trimEnd()");
console.log("*" + trimEnd(trimStr) + "*");
console.log("trim()");
console.log("*" + trim(trimStr) + "*");


const padStr: string = "pad";
console.log("padStart()");
console.log(padStart(padStr, 10, "_"));
console.log("padEnd()");
console.log(padEnd(padStr, 10, "_"));
console.log("pad()");
console.log(pad(padStr, 10, "_"));

Example #4
0
function checkForLeadingWhitespace(filePath: string, fileContents: string) {
  const fileHasLeadingWhitespace = trimStart(fileContents).length !== fileContents.length;
  const failures: Failure[] = fileHasLeadingWhitespace ? [{ filePath, message: 'File has leading whitespace.' }] : [];
  return failures;
}
Example #5
0
	return new Promise((resolve, reject) => {
		let params;
		const startTime = new Date().getTime();

		if (_.isEmpty(_self._config.target)) {
			return reject(new errors.TargetError(opts, 'Target not set'));
		}

		// Validate Opts
		_.forEach(_.pick(opts, ['startElement', 'numElements']), (value, opt) => {
			if (_hasValue(value) && !_isInteger(value)) {
				return reject(new errors.ArgumentError(opts, 'Invalid ' + opt));
			}
			return null;
		});

		// Configure Options
		let reqOpts: IRequestOptionsInternal = {
			method: (opts.method || Method.GET).toUpperCase(),
			uri: urlJoin(_self._config.target, _.trimStart(opts.uri, '/')),
			timeout: opts.timeout || _self._config.timeout,
			rejectUnauthorized: false,
			headers: { ..._self._config.headers },
			params: { ...opts.params },
			body: opts.body,
			encodeParams: _.get(opts, 'encodeParams', false),
		};

		if (_self._config.userAgent) {
			reqOpts.headers['User-Agent'] = _self._config.userAgent;
		}

		if (!opts.noAuth && !opts.auth && _self._config.token) {
			reqOpts.headers.Authorization = _self._config.token;
		}

		if (opts.mimeType) {
			reqOpts.headers.Accept = opts.mimeType;
			if (opts.method === Method.POST || opts.method === Method.PUT) {
				reqOpts.headers['Content-Type'] = opts.mimeType;
			}
		} else {
			// Default Accept to application/json
			reqOpts.headers.Accept = _.get(opts, 'headers.Accept', 'application/json');

			// Default Content-Type to application/json for POSTs and PUTs
			if (reqOpts.method === Method.POST || reqOpts.method === Method.PUT) {
				reqOpts.headers['Content-Type'] = _.get(opts, 'headers.Content-Type', 'application/json');
			}
		}

		reqOpts.headers = _.assign({}, reqOpts.headers, opts.headers);

		// Configure Parameters
		if (_hasValue(opts.startElement)) {
			reqOpts.params.start_element = (+opts.startElement).toString();
		}
		if (_hasValue(opts.numElements)) {
			reqOpts.params.num_elements = (+opts.numElements).toString();
			reqOpts.params.start_element = (+opts.startElement || reqOpts.params.start_element || 0).toString(); // startElement is required if numElements is set
		}

		params = query.stringify(reqOpts.params, {encode: reqOpts.encodeParams});

		if (params !== '') {
			reqOpts.uri += (opts.uri.indexOf('?') === -1) ? '?' : '&';
			reqOpts.uri += params;
		}

		if (_self._config.beforeRequest) {
			const beforeRequestOpts = _self._config.beforeRequest(reqOpts);
			if (beforeRequestOpts) {
				reqOpts = _.assign({}, reqOpts, beforeRequestOpts);
			}
		}

		return _self._config.request(reqOpts).then((res) => {
			const totalTime = new Date().getTime() - startTime;

			let newRes: IResponse = _.assign({
				requestTime: res.requestTime || totalTime,
				totalTime: new Date().getTime() - startTime,
			}, res);

			if (_self._config.afterRequest) {
				const afterRequestRes = _self._config.afterRequest(newRes);
				if (afterRequestRes) {
					newRes = _.assign({}, newRes, afterRequestRes);
				}
			}

			if (newRes.statusCode >= 400) {
				return reject(errors.buildError(reqOpts, newRes));
			}

			// Temporary fix
			let errorId;
			let errorCode;
			if (newRes.body && newRes.body.response && newRes.body.response) {
				errorId = newRes.body.response.error_id;
				errorCode = newRes.body.response.error_code;
			}
			if (errorId === 'SYSTEM' && errorCode === 'SERVICE_UNAVAILABLE') {
				return reject(errors.buildError(reqOpts, newRes));
			}
			if (errorId === 'SYSTEM' && errorCode === 'UNKNOWN') {
				return reject(errors.buildError(reqOpts, newRes));
			}

			newRes.req = reqOpts;

			return resolve(newRes);
		}).catch((err) => {
			let newErr;
			if (_self._config.afterRequest) {
				newErr = _self._config.afterRequest(err);
			}
			return reject(errors.buildError(reqOpts, newErr || err));
		});
	});