Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert code to ECMAScript modules #149

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Flattens the object - it'll return an object one level deep, regardless of how
nested the original object was:

``` javascript
var flatten = require('flat')
import { flatten } from 'flat'

flatten({
key1: {
Expand All @@ -38,10 +38,10 @@ flatten({

### unflatten(original, options)

Flattening is reversible too, you can call `flatten.unflatten()` on an object:
Flattening is reversible too, you can call `unflatten` on an object:

``` javascript
var unflatten = require('flat').unflatten
import { unflatten } from 'flat'

unflatten({
'three.levels.deep': 42,
Expand Down Expand Up @@ -72,7 +72,7 @@ When enabled, both `flat` and `unflatten` will preserve arrays and their
contents. This is disabled by default.

``` javascript
var flatten = require('flat')
import { flatten } from 'flat'

flatten({
this: [
Expand Down Expand Up @@ -140,7 +140,7 @@ This only makes sense on ordered arrays, and since we're overwriting data, shoul
Maximum number of nested objects to flatten.

``` javascript
var flatten = require('flat')
import { flatten } from 'flat'

flatten({
key1: {
Expand All @@ -164,8 +164,7 @@ flatten({
Transform each part of a flat key before and after flattening.

```javascript
var flatten = require('flat')
var unflatten = require('flat').unflatten
import { flatten, unflatten } from 'flat'

flatten({
key1: {
Expand Down
14 changes: 6 additions & 8 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const readline = require('readline')

const flat = require('./index')
import fs from 'node:fs'
import path from 'node:path'
import readline from 'node:readline'
import { flatten } from './index.js'

const filepath = process.argv.slice(2)[0]
if (filepath) {
// Read from file
const file = path.resolve(process.cwd(), filepath)
fs.accessSync(file, fs.constants.R_OK) // allow to throw if not readable
out(require(file))
out(JSON.parse(fs.readFileSync(file)))
} else if (process.stdin.isTTY) {
usage(0)
} else {
Expand All @@ -27,7 +25,7 @@ if (filepath) {
}

function out (data) {
process.stdout.write(JSON.stringify(flat(data), null, 2))
process.stdout.write(JSON.stringify(flatten(data), null, 2))
}

function usage (code) {
Expand Down
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
module.exports = flatten
flatten.flatten = flatten
flatten.unflatten = unflatten

function isBuffer (obj) {
return obj &&
obj.constructor &&
Expand All @@ -13,7 +9,7 @@ function keyIdentity (key) {
return key
}

function flatten (target, opts) {
export function flatten (target, opts) {
opts = opts || {}

const delimiter = opts.delimiter || '.'
Expand Down Expand Up @@ -51,7 +47,7 @@ function flatten (target, opts) {
return output
}

function unflatten (target, opts) {
export function unflatten (target, opts) {
opts = opts || {}

const delimiter = opts.delimiter || '.'
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"name": "flat",
"version": "5.0.2",
"type": "module",
"main": "index.js",
"exports": "./index.js",
"bin": "cli.js",
"sideEffects": false,
"engines": {
"node": ">=14.16"
},
"scripts": {
"test": "mocha -u tdd --reporter spec && standard cli.js index.js test/test.js"
},
Expand Down
36 changes: 18 additions & 18 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* globals suite test */
import assert from 'node:assert'
import { exec } from 'node:child_process'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { flatten, unflatten } from '../index.js'

const assert = require('assert')
const path = require('path')
const { exec } = require('child_process')
const pkg = require('../package.json')
const flat = require('../index')

const flatten = flat.flatten
const unflatten = flat.unflatten
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const pkg = JSON.parse(readFileSync('./package.json'))

const primitives = {
String: 'good morning',
Expand Down Expand Up @@ -361,22 +361,22 @@ suite('Unflatten', function () {

suite('Overwrite + non-object values in key positions', function () {
test('non-object keys + overwrite should be overwritten', function () {
assert.deepStrictEqual(flat.unflatten({ a: null, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(flat.unflatten({ a: 0, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(flat.unflatten({ a: 1, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(flat.unflatten({ a: '', 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(unflatten({ a: null, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(unflatten({ a: 0, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(unflatten({ a: 1, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(unflatten({ a: '', 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
})

test('overwrite value should not affect undefined keys', function () {
assert.deepStrictEqual(flat.unflatten({ a: undefined, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(flat.unflatten({ a: undefined, 'a.b': 'c' }, { overwrite: false }), { a: { b: 'c' } })
assert.deepStrictEqual(unflatten({ a: undefined, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
assert.deepStrictEqual(unflatten({ a: undefined, 'a.b': 'c' }, { overwrite: false }), { a: { b: 'c' } })
})

test('if no overwrite, should ignore nested values under non-object key', function () {
assert.deepStrictEqual(flat.unflatten({ a: null, 'a.b': 'c' }), { a: null })
assert.deepStrictEqual(flat.unflatten({ a: 0, 'a.b': 'c' }), { a: 0 })
assert.deepStrictEqual(flat.unflatten({ a: 1, 'a.b': 'c' }), { a: 1 })
assert.deepStrictEqual(flat.unflatten({ a: '', 'a.b': 'c' }), { a: '' })
assert.deepStrictEqual(unflatten({ a: null, 'a.b': 'c' }), { a: null })
assert.deepStrictEqual(unflatten({ a: 0, 'a.b': 'c' }), { a: 0 })
assert.deepStrictEqual(unflatten({ a: 1, 'a.b': 'c' }), { a: 1 })
assert.deepStrictEqual(unflatten({ a: '', 'a.b': 'c' }), { a: '' })
})
})

Expand Down