Skip to content

Commit

Permalink
Migrate to biome
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Feb 1, 2024
1 parent f8128db commit a3bef83
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 184 deletions.
5 changes: 0 additions & 5 deletions .eslintignore

This file was deleted.

12 changes: 0 additions & 12 deletions .eslintrc.json

This file was deleted.

6 changes: 0 additions & 6 deletions .prettierrc.json

This file was deleted.

32 changes: 32 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"javascript": {
"formatter": {
"arrowParentheses": "always",
"indentStyle": "space",
"semicolons": "asNeeded",
"trailingComma": "all",
"lineWidth": 100,
"quoteStyle": "single"
}
},
"linter": {
"rules": {
"style": {
"useConst": "off",
"noVar": "off",
"noParameterAssign": "off",
"useTemplate": "off"
},
"correctness": {
"noInnerDeclarations": "off"
},
"suspicious": {
"noGlobalIsNan": "off"
},
"complexity": {
"useLiteralKeys": "off"
}
}
}
}
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { generateFieldMask, applyFieldMask } = require('./lib/fieldmask');
const { generateFieldMask, applyFieldMask } = require('./lib/fieldmask')

module.exports.generateFieldMask = generateFieldMask;
module.exports.applyFieldMask = applyFieldMask;
module.exports.generateFieldMask = generateFieldMask
module.exports.applyFieldMask = applyFieldMask
40 changes: 20 additions & 20 deletions lib/escaping.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
function escape(propertyName) {
return propertyName.replace(/\\/g, '\\\\').replace(/\./g, '\\.');
function escapeProperty(propertyName) {
return propertyName.replace(/\\/g, '\\\\').replace(/\./g, '\\.')
}

function unescapeAndSplit(originalPath) {
const properties = [];
let path = originalPath;
let i = path.indexOf('.');
const properties = []
let path = originalPath
let i = path.indexOf('.')
while (i >= 0) {
if (isEscapedDot(path, i)) {
path = unescapeFirstDotChar(path);
path = unescapeFirstDotChar(path)
// Find the index of the first dot AFTER the one we just unescaped
i = path.indexOf('.', i);
i = path.indexOf('.', i)
} else {
const firstProperty = path.substring(0, i);
properties.push(unescapeEscapeChars(firstProperty));
path = path.substring(i + 1);
i = path.indexOf('.');
const firstProperty = path.substring(0, i)
properties.push(unescapeEscapeChars(firstProperty))
path = path.substring(i + 1)
i = path.indexOf('.')
}
}

properties.push(unescapeEscapeChars(path));
return properties;
properties.push(unescapeEscapeChars(path))
return properties
}

function unescapeFirstDotChar(str) {
return str.replace('\\.', '.');
return str.replace('\\.', '.')
}

function unescapeEscapeChars(str) {
return str.replace(/\\\\/g, '\\');
return str.replace(/\\\\/g, '\\')
}

function isEscapedDot(path, i) {
let counter = 0;
let counter = 0
while (path.substring(i - 1, i) === '\\') {
counter++;
i--;
counter++
i--
}

return counter % 2 === 1;
return counter % 2 === 1
}

module.exports = { escape, unescapeAndSplit };
module.exports = { escapeProperty, unescapeAndSplit }
48 changes: 24 additions & 24 deletions lib/fieldmask.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const get = require('lodash/get');
const set = require('lodash/set');
const { escape, unescapeAndSplit } = require('./escaping');
const get = require('lodash/get')
const set = require('lodash/set')
const { escapeProperty, unescapeAndSplit } = require('./escaping')

/**
* Creates a new object that copies fields present in field mask from specified source object
Expand All @@ -10,17 +10,17 @@ const { escape, unescapeAndSplit } = require('./escaping');
*/
function applyFieldMask(sourceObject, fieldMask) {
if (!_isObject(sourceObject)) {
return sourceObject;
return sourceObject
}
const result = {};
const result = {}

for (let i = 0; i < fieldMask.length; i++) {
const path = unescapeAndSplit(fieldMask[i]);
const sourceValue = get(sourceObject, path);
set(result, path, sourceValue);
const path = unescapeAndSplit(fieldMask[i])
const sourceValue = get(sourceObject, path)
set(result, path, sourceValue)
}

return result;
return result
}

/**
Expand All @@ -29,50 +29,50 @@ function applyFieldMask(sourceObject, fieldMask) {
* @returns {string[]} - generated field mask
*/
function generateFieldMask(object) {
const paths = [];
const paths = []
if (!_isObject(object)) {
return paths;
return paths
}

const pathBuilder = '';
_generatePathForObject(object, pathBuilder, paths);
return paths;
const pathBuilder = ''
_generatePathForObject(object, pathBuilder, paths)
return paths
}

function _generatePathForObject(object, path, paths) {
for (let property in object) {
if (Object.prototype.hasOwnProperty.call(object, property)) {
let expandedPath = path.length > 0 ? path + '.' : path;
expandedPath += escape(property);
let expandedPath = path.length > 0 ? path + '.' : path
expandedPath += escapeProperty(property)

const objProperty = object[property];
const objProperty = object[property]

if (_isDate(object[property])) {
paths.push(expandedPath);
paths.push(expandedPath)
}

if (_isObject(objProperty) && !Array.isArray(objProperty)) {
_generatePathForObject(objProperty, expandedPath, paths);
_generatePathForObject(objProperty, expandedPath, paths)
} else if (!_isFunction(objProperty)) {
paths.push(expandedPath);
paths.push(expandedPath)
}
}
}
}

function _isDate(object) {
return object instanceof Date && !isNaN(object);
return object instanceof Date && !isNaN(object)
}

function _isObject(object) {
return typeof object === 'object' && object !== null;
return typeof object === 'object' && object !== null
}

function _isFunction(object) {
return typeof object === 'function';
return typeof object === 'function'
}

module.exports = {
applyFieldMask,
generateFieldMask,
};
}
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"coveralls": "nyc report --reporter=lcov",
"test": "mocha ./test",
"test:coverage": "nyc npm test",
"lint": "eslint .",
"lint": "npx @biomejs/[email protected] lint index.js lib test biome.json",
"lint:fix": "npx @biomejs/[email protected] check --apply index.js lib test biome.json",
"prettier": "prettier --write \"{lib,test}/**/*.{js,ts}\""
},
"repository": {
Expand All @@ -36,9 +37,6 @@
},
"devDependencies": {
"chai": "^4.3.7",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^3.4.1",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"prettier": "^2.2.1"
Expand Down
62 changes: 31 additions & 31 deletions test/escaping.spec.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
const { assert } = require('chai');
const { escape, unescapeAndSplit } = require('../lib/escaping');
const { assert } = require('chai')
const { escapeProperty, unescapeAndSplit } = require('../lib/escaping')

describe('Escaping', () => {
describe('escape', () => {
it('should return property name as is when it does not contain special characters', () => {
assert.equal(escape('foo'), 'foo');
});
assert.equal(escapeProperty('foo'), 'foo')
})

it('should escape dots in the property name', () => {
assert.equal(escape('foo.bar'), 'foo\\.bar');
});
assert.equal(escapeProperty('foo.bar'), 'foo\\.bar')
})

it('should escape multiple dots in the property name', () => {
assert.equal(escape('foo.bar.baz.'), 'foo\\.bar\\.baz\\.');
});
assert.equal(escapeProperty('foo.bar.baz.'), 'foo\\.bar\\.baz\\.')
})

it('should escape the escape char', () => {
assert.equal(escape('foo\\bar'), 'foo\\\\bar');
});
assert.equal(escapeProperty('foo\\bar'), 'foo\\\\bar')
})

it('should escape the escape char when containing with dots', () => {
assert.equal(escape('foo\\.bar'), 'foo\\\\\\.bar');
});
});
assert.equal(escapeProperty('foo\\.bar'), 'foo\\\\\\.bar')
})
})

describe('unescapeAndSplit', () => {
it('should array with single element when path is for a single property', () => {
assert.deepEqual(unescapeAndSplit('foo'), ['foo']);
});
assert.deepEqual(unescapeAndSplit('foo'), ['foo'])
})

it('should return an array of names split by dot', () => {
assert.deepEqual(unescapeAndSplit('foo.bar.baz'), ['foo', 'bar', 'baz']);
});
assert.deepEqual(unescapeAndSplit('foo.bar.baz'), ['foo', 'bar', 'baz'])
})

it('should unescape escaped dots', () => {
assert.deepEqual(unescapeAndSplit('foo\\.bar'), ['foo.bar']);
});
assert.deepEqual(unescapeAndSplit('foo\\.bar'), ['foo.bar'])
})

it('should return an array of names split by dot with respect to escaped dots', () => {
assert.deepEqual(unescapeAndSplit('foo\\.bar\\.baz.baf'), ['foo.bar.baz', 'baf']);
});
assert.deepEqual(unescapeAndSplit('foo\\.bar\\.baz.baf'), ['foo.bar.baz', 'baf'])
})

it('should handle trailing escaped dot correctly', () => {
assert.deepEqual(unescapeAndSplit('foo\\..baz'), ['foo.', 'baz']);
});
assert.deepEqual(unescapeAndSplit('foo\\..baz'), ['foo.', 'baz'])
})

it('should handle escape char correctly', () => {
assert.deepEqual(unescapeAndSplit('foo\\\\.baz'), ['foo\\', 'baz']);
});
assert.deepEqual(unescapeAndSplit('foo\\\\.baz'), ['foo\\', 'baz'])
})

it('should handle escape char with dot correctly', () => {
assert.deepEqual(unescapeAndSplit('foo\\\\\\..baz'), ['foo\\.', 'baz']);
});
assert.deepEqual(unescapeAndSplit('foo\\\\\\..baz'), ['foo\\.', 'baz'])
})

it('should play well with escape function results', () => {
assert.deepEqual(unescapeAndSplit(escape('foo\\.bar')), ['foo\\.bar']);
});
});
});
assert.deepEqual(unescapeAndSplit(escapeProperty('foo\\.bar')), ['foo\\.bar'])
})
})
})
Loading

0 comments on commit a3bef83

Please sign in to comment.