@svizzle/file/write

Methods

(static) saveExportedObj(jsFilepath, indentopt) → {function}

Source:
Since:
  • 0.14.0
See:

[node environment] Return a function that expects an object and returns a promise that writes the input object as an exported object in a javascript file to the provided filepath.

Example
> obj = {a: [1, 2], b: [3, 4]}
>
> promiseThatReturnsTheObject()
.then(saveExportedObj('destination/path.js'))
.catch(err => console.error(err));

$ cat destination/path.js
export default {"a": [1, 2], "b": [3, 4]};

> promiseThatReturnsTheObject()
.then(saveExportedObj('destination/path.js', 2))
.catch(err => console.error(err));

$ cat destination/path.js
export default {
  "a": [
    1,
    2
  ],
  "b": [
    3,
    4
  ]
};

> promiseThatReturnsTheObject()
.then(saveExportedObj('destination/path.js', '\t'))
.catch(err => console.error(err));

$ cat destination/path.js
export default {
	"a": [
		1,
		2
	],
	"b": [
		3,
		4
	]
};
Parameters:
Name Type Attributes Default Description
jsFilepath string

The javascript filepath where to save the expected object

indent number | string <optional>
0

The amount of spaces or the string to be used to indent

Returns:
  • Object -> Promise - @sideEffects: fs.writeFile
Type
function

(static) saveExportedObjects(outputs) → {promise}

Source:
Since:
  • 0.14.0
See:

[node environment] Return a promise that resolves when all the provided objects have been written as javascript files in the correspondent file paths, each with the provided indentation.

Example
> saveExportedObjects([
	{
		filepath: filepath1,
		object: object1
	},
	{
		filepath: filepath2,
		object: object2,
		indentation: 2
	},
])
.catch(err => console.error(err));
Parameters:
Name Type Description
outputs Array.<Object>

{filepath, object, indentation}[]

Properties
Name Type Attributes Default Description
filepath string

path where to save object

object string

object to write in filepath

indentation number | string <optional>
0

The amount of spaces or the string to be used to indent

Returns:
  • @sideEffects: fs.writeFile, console.log
Type
promise

(static) saveExportedObjPassthrough(jsFilepath, indentopt) → {function}

Source:
Since:
  • 0.14.0
See:

[node environment] Return a function that expects an object and returns a promise that writes the input object as an exported object in a javascript file to the provided filepath and returns the object.

Example
> obj = {a: [1, 2], b: [3, 4]}
>
> promiseThatReturnsTheObject()
.then(saveExportedObjPassthrough('destination/path.js'))
.then(x => console.log(x))
.catch(err => console.error(err));
{a: [1, 2], b: [3, 4]}

$ cat destination/path.js
export default {"a": [1, 2], "b": [3, 4]};

> promiseThatReturnsTheObject()
.then(saveExportedObjPassthrough('destination/path.js', 2))
.then(x => console.log(x))
.catch(err => console.error(err));
{a: [1, 2], b: [3, 4]}

$ cat destination/path.js
export default {
  "a": [
    1,
    2
  ],
  "b": [
    3,
    4
  ]
};

> promiseThatReturnsTheObject()
.then(saveExportedObj('destination/path.js', '\t'))
.then(x => console.log(x))
.catch(err => console.error(err));
{a: [1, 2], b: [3, 4]}

$ cat destination/path.js
export default {
	"a": [
		1,
		2
	],
	"b": [
		3,
		4
	]
};
Parameters:
Name Type Attributes Default Description
jsFilepath string

The javascript filepath where to save the expected object

indent number | string <optional>
0

The amount of spaces or the string to be used to indent

Returns:
  • Object -> Promise - @sideEffects: fs.writeFile
Type
function

(static) saveObj(filepath, indentopt) → {function}

Source:
Since:
  • 0.1.0
See:

[node environment] Return a function that expects an object and returns a promise that writes the input object to the provided filepath.

Example
> obj = {a: 1, b: 2}
> promiseThatReturnsTheObject()
.then(saveObj('destination/path.json'))
.catch(err => console.error(err));

$ cat destination/path.json
{"a": 1, "b": 2}

> promiseThatReturnsTheObject()
.then(saveObj('destination/path.json', 2))
.catch(err => console.error(err));

$ cat destination/path.json
{
  "a": 1,
  "b": 2
}

> promiseThatReturnsTheObject()
.then(saveObj('destination/path.json', '\t'))
.catch(err => console.error(err));

$ cat destination/path.json
{
	"a": 1,
	"b": 2
}
Parameters:
Name Type Attributes Default Description
filepath string

The filepath where to save the expected object

indent number | string <optional>
0

The amount of spaces or the string to be used to indent

Returns:
  • Object -> Promise - @sideEffects: fs.writeFile
Type
function

(static) saveObjects(outputs) → {promise}

Source:
Since:
  • 0.12.0
See:

[node environment] Return a promise that resolves when all the provided objects have been written in the correspondent file paths, each with the provided indentation.

Example
> saveObjects([
	{
		filepath: filepath1,
		object: object1
	},
	{
		filepath: filepath2,
		object: object2,
		indentation: 2
	},
	{
		filepath: filepath3,
		object: object3,
		indentation: '\t'
	}
])
.catch(err => console.error(err));
Parameters:
Name Type Description
outputs Array.<Object>

{filepath, object, indentation}[]

Properties
Name Type Attributes Default Description
filepath string

path where to save object

object string

object to write in filepath

indentation number | string <optional>
0

The amount of spaces or the string to be used to indent

Returns:
  • Object -> Promise - @sideEffects: fs.writeFile, console.log
Type
promise

(static) saveObjPassthrough(filepath, indentopt) → {function}

Source:
Since:
  • 0.1.0
See:

[node environment] Return a function that expects an object and returns a promise that writes the input object to the provided filepath and returns the object.

Example
> obj = {a: 1, b: 2}
> promiseThatReturnsTheObject()
.then(saveObjPassthrough('destination/path.json'))
.then(x => console.log(x))
.catch(err => console.error(err));
{a: 1, b: 2}

$ cat destination/path.js
export default {"a": 1, "b": 2};

> promiseThatReturnsTheObject()
.then(saveObjPassthrough('destination/path.json', 2))
.then(x => console.log(x))
.catch(err => console.error(err));
{a: 1, b: 2}

$ cat destination/path.js
export default {
  "a": 1,
  "b": 2
};

> promiseThatReturnsTheObject()
.then(saveObjPassthrough('destination/path.json', '\t'))
.then(x => console.log(x))
.catch(err => console.error(err));
{a: 1, b: 2}

$ cat destination/path.js
export default {
	"a": 1,
	"b": 2
};
Parameters:
Name Type Attributes Default Description
filepath string

The filepath where to save the expected object

indent number | string <optional>
0

The amount of spaces or the string to be used to indent

Returns:
  • Object -> Promise - @sideEffects: fs.writeFile
Type
function

(static) saveResponse(filepath) → {function}

Source:
Since:
  • 0.1.0

[node environment] Return a function that expects a response and returns a promise that saves the response body to the provided filepath.

Example
> promiseThatReturnsAResponse()
.then(saveResponse('destination/path'))
.catch(err => console.error(err));
Parameters:
Name Type Description
filepath string

The filepath where to save the body of the expected response.

Returns:
  • Response -> Promise - @sideEffects: fs.createWriteStream
Type
function

(static) saveString(filepath) → {function}

Source:
Since:
  • 0.7.0
See:

[node environment] Return a function that expects a string and returns a promise that writes to the provided filepath.

Example
> promiseThatReturnsAString()
.then(saveString('destination/path'))
.catch(err => console.error(err));
Parameters:
Name Type Description
filepath string

The filepath where to save the expected string

Returns:
  • String -> Promise - @sideEffects: fs.writeFile
Type
function

(static) saveStringPassthrough(filepath) → {function}

Source:
Since:
  • 0.7.0
See:

[node environment] Return a function that expects a string and returns a promise that writes to the provided filepath and returns the string.

Example
> promiseThatReturnsAString()
.then(saveStringPassthrough('destination/path'))
.then(x => console.log(x))
.catch(err => console.error(err));
Parameters:
Name Type Description
filepath string

The filepath where to save the expected string

Returns:
  • String -> Promise - @sideEffects: fs.writeFile
Type
function

(static) saveYaml(yamlPath) → {function}

Source:
Since:
  • 0.13.0
See:

[node environment] Return a function that expects an object and returns a promise that writes to the provided YAML filepath.

Example
> obj = {a: 1, b: 2}
> promiseThatReturnsTheObject()
.then(saveYaml('destination/path.yaml'))
.catch(err => console.error(err));

$ cat destination/path.yaml
- a: 1
- b: 2
Parameters:
Name Type Description
yamlPath string

The YAML filepath where to save the expected object

Returns:
  • Object -> Promise - @sideEffects: fs.writeFile
Type
function

(static) saveYamlPassthrough(yamlPath) → {function}

Source:
Since:
  • 0.13.0
See:

[node environment] Return a function that expects an object and returns a promise that writes to the provided YAML filepath and returns the object.

Example
> obj = {a: 1, b: 2}
> promiseThatReturnsTheObject()
.then(saveYamlPassthrough('destination/path.yaml'))
.then(x => console.log(x))
.catch(err => console.error(err));
{a: 1, b: 2}

$ cat destination/path.yaml
- a: 1
- b: 2
Parameters:
Name Type Description
yamlPath string

The YAML filepath where to save the expected object

Returns:
  • Object -> Promise - @sideEffects: fs.writeFile
Type
function

(static) writeFile(filePath, data, encodingopt) → {promise}

Source:
Since:
  • 0.5.0

[node environment] Return a promise that writes the provided string to the provided path.

Example
> writeFile('./foo.txt', JSON.stringify({a: 1, b: 2, c:3}))
.then(() => console.log('saved'))
.catch(err => console.error(err));

$ cat foo.txt
{'a':1,'b':2,'c':3}
$ base64 foo.txt
eyJhIjoxLCJiIjoyLCJjIjozfQ==

> writeFile('./foo.txt', JSON.stringify({a: 1, b: 2, c:3}), 'base64')
.then(x => console.log(x))
.catch(err => console.error(err));

$ cat foo.txt
kV?s
$ base64 foo.txt
a1b2cw==
Parameters:
Name Type Attributes Default Description
filePath string
data string | Buffer | Uint8Array

item to write to file

encoding string <optional>
'utf8'

Encoding supported by Node Buffer

Returns:
  • @sideEffects: fs.writeFile
Type
promise