Skip to content

Commit

Permalink
Convert var to const across source.
Browse files Browse the repository at this point in the history
  • Loading branch information
timoxley committed Oct 22, 2019
1 parent fdfd095 commit 533ac93
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
54 changes: 27 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ function keyIdentity (key) {
function flatten (target, opts) {
opts = opts || {}

var delimiter = opts.delimiter || '.'
var maxDepth = opts.maxDepth
var transformKey = opts.transformKey || keyIdentity
var output = {}
const delimiter = opts.delimiter || '.'
const maxDepth = opts.maxDepth
const transformKey = opts.transformKey || keyIdentity
const output = {}

function step (object, prev, currentDepth) {
currentDepth = currentDepth || 1
Object.keys(object).forEach(function (key) {
var value = object[key]
var isarray = opts.safe && Array.isArray(value)
var type = Object.prototype.toString.call(value)
var isbuffer = isBuffer(value)
var isobject = (
const value = object[key]
const isarray = opts.safe && Array.isArray(value)
const type = Object.prototype.toString.call(value)
const isbuffer = isBuffer(value)
const isobject = (
type === '[object Object]' ||
type === '[object Array]'
)

var newKey = prev
const newKey = prev
? prev + delimiter + transformKey(key)
: transformKey(key)

Expand All @@ -49,20 +49,20 @@ function flatten (target, opts) {
function unflatten (target, opts) {
opts = opts || {}

var delimiter = opts.delimiter || '.'
var overwrite = opts.overwrite || false
var transformKey = opts.transformKey || keyIdentity
var result = {}
const delimiter = opts.delimiter || '.'
const overwrite = opts.overwrite || false
const transformKey = opts.transformKey || keyIdentity
const result = {}

var isbuffer = isBuffer(target)
const isbuffer = isBuffer(target)
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
return target
}

// safely ensure that the key is
// an integer.
function getkey (key) {
var parsedKey = Number(key)
const parsedKey = Number(key)

return (
isNaN(parsedKey) ||
Expand All @@ -81,9 +81,9 @@ function unflatten (target, opts) {
}

function isEmpty (val) {
var type = Object.prototype.toString.call(val)
var isArray = type === '[object Array]'
var isObject = type === '[object Object]'
const type = Object.prototype.toString.call(val)
const isArray = type === '[object Array]'
const isObject = type === '[object Object]'

if (!val) {
return true
Expand All @@ -95,8 +95,8 @@ function unflatten (target, opts) {
}

target = Object.keys(target).reduce((result, key) => {
var type = Object.prototype.toString.call(target[key])
var isObject = (type === '[object Object]' || type === '[object Array]')
const type = Object.prototype.toString.call(target[key])
const isObject = (type === '[object Object]' || type === '[object Array]')
if (!isObject || isEmpty(target[key])) {
result[key] = target[key]
return result
Expand All @@ -110,14 +110,14 @@ function unflatten (target, opts) {
}, {})

Object.keys(target).forEach(function (key) {
var split = key.split(delimiter).map(transformKey)
var key1 = getkey(split.shift())
var key2 = getkey(split[0])
var recipient = result
const split = key.split(delimiter).map(transformKey)
let key1 = getkey(split.shift())
let key2 = getkey(split[0])
let recipient = result

while (key2 !== undefined) {
var type = Object.prototype.toString.call(recipient[key1])
var isobject = (
const type = Object.prototype.toString.call(recipient[key1])
const isobject = (
type === '[object Object]' ||
type === '[object Array]'
)
Expand Down
30 changes: 16 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* globals suite test */

var assert = require('assert')
var flat = require('../index')
var flatten = flat.flatten
var unflatten = flat.unflatten
const assert = require('assert')
const path = require('path')
const flat = require('../index')

var primitives = {
const flatten = flat.flatten
const unflatten = flat.unflatten

const primitives = {
String: 'good morning',
Number: 1234.99,
Boolean: true,
Expand All @@ -16,7 +18,7 @@ var primitives = {

suite('Flatten Primitives', function () {
Object.keys(primitives).forEach(function (key) {
var value = primitives[key]
const value = primitives[key]

test(key, function () {
assert.deepStrictEqual(flatten({
Expand All @@ -32,7 +34,7 @@ suite('Flatten Primitives', function () {

suite('Unflatten Primitives', function () {
Object.keys(primitives).forEach(function (key) {
var value = primitives[key]
const value = primitives[key]

test(key, function () {
assert.deepStrictEqual(unflatten({
Expand Down Expand Up @@ -252,7 +254,7 @@ suite('Unflatten', function () {
})

test('nested objects do not clobber each other when a.b inserted before a', function () {
var x = {}
const x = {}
x['foo.bar'] = { t: 123 }
x.foo = { p: 333 }
assert.deepStrictEqual(unflatten(x), {
Expand Down Expand Up @@ -425,7 +427,7 @@ suite('Unflatten', function () {

suite('.object', function () {
test('Should create object instead of array when true', function () {
var unflattened = unflatten({
const unflattened = unflatten({
'hello.you.0': 'ipsum',
'hello.you.1': 'lorem',
'hello.other.world': 'foo'
Expand All @@ -445,7 +447,7 @@ suite('Unflatten', function () {
})

test('Should create object instead of array when nested', function () {
var unflattened = unflatten({
const unflattened = unflatten({
hello: {
'you.0': 'ipsum',
'you.1': 'lorem',
Expand All @@ -467,7 +469,7 @@ suite('Unflatten', function () {
})

test('Should keep the zero in the left when object is true', function () {
var unflattened = unflatten({
const unflattened = unflatten({
'hello.0200': 'world',
'hello.0500': 'darkness my old friend'
}, {
Expand All @@ -483,7 +485,7 @@ suite('Unflatten', function () {
})

test('Should not create object when false', function () {
var unflattened = unflatten({
const unflattened = unflatten({
'hello.you.0': 'ipsum',
'hello.you.1': 'lorem',
'hello.other.world': 'foo'
Expand Down Expand Up @@ -571,7 +573,7 @@ suite('Arrays', function () {

suite('Order of Keys', function () {
test('Order of keys should not be changed after round trip flatten/unflatten', function () {
var obj = {
const obj = {
b: 1,
abc: {
c: [{
Expand All @@ -582,7 +584,7 @@ suite('Order of Keys', function () {
},
a: 1
}
var result = unflatten(
const result = unflatten(
flatten(obj)
)

Expand Down

0 comments on commit 533ac93

Please sign in to comment.