From 3d9ec87bc334fd87bc0b55e0307e06767954baee Mon Sep 17 00:00:00 2001 From: Jonas Hermsmeier Date: Wed, 29 Nov 2017 15:52:09 +0100 Subject: [PATCH 1/2] refactor: Remove lodash This removes use of lodash throughout. Change-Type: patch --- lib/parse.js | 92 ++++++++++++++++++++----------------------- package.json | 3 +- tests/execute.spec.js | 5 +-- 3 files changed, 45 insertions(+), 55 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 71c46099..aa0f5b6a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -16,7 +16,6 @@ 'use strict'; -const _ = require('lodash'); const yaml = require('js-yaml'); /** @@ -58,65 +57,58 @@ const yaml = require('js-yaml'); * > } * > ] */ - module.exports = (input) => { - if (_.isEmpty(_.trim(input))) { + if (!input || !input.trim()) { return []; } - return _.compact(_.map(input.split(/\n\s*\n/), (device) => { - device = _.chain(device) - .split('\n') - .filter((line) => { - return /^(\s\s-\s)?[a-z]+:/i.test(line); - }) - .map((line) => { - return line - .replace(/\\[^.\\]/g, (match, index, string) => { - const escapedCharacter = _.last(match); - - if (string[index - 1] === '\\' || _.includes([ - 'b', - 'f', - 'n', - 'r', - 't', - 'v' - ], escapedCharacter)) { - return match; - } + const allowedEscapes = [ 'b', 'f', 'n', 'r', 't', 'v' ]; - return escapedCharacter; - }) + return input.split(/\n\s*\n/g) + .map((device) => { + device = device.split(/\r?\n/g) + .filter((line) => { + return /^(\s\s-\s)?[a-z]+:/i.test(line); + }) + .map((line) => { + return line + .replace(/\\[^.\\]/g, (match, index, string) => { + const escapedCharacter = match[match.length - 1]; - // Remove non printable ascii characters - // See http://stackoverflow.com/a/24229554 - .replace(/[^\x20-\x7E]+/g, '') + // Remove non printable ascii characters + // See http://stackoverflow.com/a/24229554 + if (string[index - 1] === '\\' || allowedEscapes.indexOf(escapedCharacter) !== -1) { + return match; + } - .replace(/"/g, (match, index, string) => { - if (_.some([ - string.indexOf('"') === index, - string.lastIndexOf('"') === index - ])) { - return match; - } + return escapedCharacter; + }) + .replace(/[^\x20-\x7E]+/g, '') + .replace(/"/g, (match, index, string) => { + if (string.indexOf('"') === index || string.lastIndexOf('"') === index) { + return match; + } - return '\\"'; - }); - }) - .join('\n') - .value(); + return '\\"'; + }); + }) + .join('\n'); - const result = yaml.safeLoad(device); + const result = yaml.safeLoad(device); - if (_.isString(result)) { - return _.object([ result ], [ null ]); - } + if (typeof result === 'string') { + const data = {}; + data[result] = null; + return data; + } - if (!result || !result.device) { - return; - } + if (!result || !result.device) { + return null; + } - return result; - })); + return result; + }) + .filter((result) => { + return Boolean(result); + }); }; diff --git a/package.json b/package.json index 035621a3..90105640 100644 --- a/package.json +++ b/package.json @@ -51,8 +51,7 @@ "bindings": "^1.3.0", "debug": "^3.1.0", "js-yaml": "^3.10.0", - "lodash": "^4.16.4", "nan": "^2.7.0", "prebuild-install": "^2.3.0" } -} \ No newline at end of file +} diff --git a/tests/execute.spec.js b/tests/execute.spec.js index 12d267bd..b0e325da 100644 --- a/tests/execute.spec.js +++ b/tests/execute.spec.js @@ -17,7 +17,6 @@ 'use strict'; const m = require('mochainon'); -const _ = require('lodash'); const os = require('os'); const childProcess = require('child_process'); const execute = require('../lib/execute'); @@ -27,7 +26,7 @@ describe('Execute', function() { describe('.extractAndRun()', function() { it('should be able to execute a script', function(done) { - const script = _.attempt(() => { + const script = (() => { if (os.platform() === 'win32') { return { content: '@echo off\necho foo bar baz', @@ -39,7 +38,7 @@ describe('Execute', function() { content: '#!/bin/bash\necho "foo bar baz"', originalFilename: 'hello.sh' }; - }); + })(); execute.extractAndRun(script, (error, output) => { m.chai.expect(error).to.not.exist; From 38e26aefa327cb6c247ac1f8205e3b8ea5fcaa0f Mon Sep 17 00:00:00 2001 From: "resin-io-modules-versionbot[bot]" Date: Thu, 14 Dec 2017 08:52:28 +0000 Subject: [PATCH 2/2] v5.2.11 --- CHANGELOG.md | 4 ++++ package.json | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 015111b5..10e07a45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to [Semantic Versioning](http://semver.org/). +## v5.2.11 - 2017-12-14 + +* Refactor: Remove lodash #235 [Jonas Hermsmeier] + ## v5.2.10 - 2017-12-12 * Fix(package): Fix prebuild script being run on build #236 [Jonas Hermsmeier] diff --git a/package.json b/package.json index 90105640..97d7329c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "drivelist", - "version": "5.2.10", + "version": "5.2.11", "description": "List all connected drives in your computer, in all major operating systems", "main": "lib/drivelist.js", "homepage": "https://github.com/resin-io-modules/drivelist", @@ -54,4 +54,4 @@ "nan": "^2.7.0", "prebuild-install": "^2.3.0" } -} +} \ No newline at end of file