Skip to content

Commit

Permalink
* chore(README.md): update function documentation and reorder functions
Browse files Browse the repository at this point in the history
* fix(index.js): fix indentation and remove unused code
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions and add new tests
* test(file-easy.test.js): update test descriptions
  • Loading branch information
ioncakephper committed Dec 7, 2024
1 parent 67927bb commit 9443c1d
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 134 deletions.
56 changes: 35 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,55 +89,69 @@ fileEasy.saveDocument(filename, content)
## Functions
<dl>
<dt><a href="#setDefaultExtension">setDefaultExtension(filename, extension)</a> ⇒ <code>string</code></dt>
<dd><p>Append specified extension if needed.</p>
</dd>
<dt><a href="#saveDocument">saveDocument(filename, content)</a></dt>
<dd><p>Save content in a file using utf8 format.</p>
</dd>
<dt><a href="#setDefaultExtension">setDefaultExtension(filename, extension)</a> ⇒ <code>string</code></dt>
<dd><p>Append specified extension if needed.</p>
</dd>
<dt><a href="#slug">slug(s)</a> ⇒ <code>string</code></dt>
<dd><p>Convert a string into an identifier.</p>
</dd>
</dl>
<a name="setDefaultExtension"></a>
<a name="saveDocument"></a>
## setDefaultExtension(filename, extension) ⇒ <code>string</code>
Append specified extension if needed.
## saveDocument(filename, content)
Save content in a file using utf8 format.
**Kind**: global function
**Throws**:
- <code>Error</code> If input is invalid or if there was an error during the save process.
**Kind**: global function
**Returns**: <code>string</code> - filename with either existing or specified extension
| Param | Type | Description |
| --- | --- | --- |
| filename | <code>string</code> | the filename to check for an existing extension. |
| extension | <code>string</code> | the extension to append if filename has no extension. It should start with a dot (e.g. `.txt`) |
| filename | <code>string</code> | The filename to create. It can also include a path ending with the filename. Path will be created if not exists. |
| content | <code>string</code> | The content to place in the file. |
<a name="saveDocument"></a>
<a name="setDefaultExtension"></a>
## saveDocument(filename, content)
Save content in a file using utf8 format.
## setDefaultExtension(filename, extension) ⇒ <code>string</code>
Append specified extension if needed.
**Kind**: global function
**Returns**: <code>string</code> - filename with either existing or specified extension
**Throws**:
- <code>TypeError</code> If filename or extension is not a string.
- <code>Error</code> If extension is empty or doesn't start with a dot.

**Kind**: global function

| Param | Type | Description |
| --- | --- | --- |
| filename | <code>string</code> | The filename to create. It can also include a path ending with the filename. Path will be created if not exists. |
| content | <code>string</code> | The content to place in the file. |
| filename | <code>string</code> | The filename to check for an existing extension. |
| extension | <code>string</code> | The extension to append if filename has no extension. It should start with a dot (e.g. `.txt`) |

<a name="slug"></a>

## slug(s) ⇒ <code>string</code>
Convert a string into an identifier.

**Kind**: global function
**Returns**: <code>string</code> - The identifier string
**Kind**: global function
**Returns**: <code>string</code> - The identifier string.

The following operations are performed on the string:
1. Trim and convert to lower case.
2. Remove diacritics (transliterate or remove non-ASCII characters).
3. Replace spaces and special characters with hyphens, allowing underscores.
4. Replace multiple hyphens with a single hyphen.
5. Remove leading and trailing hyphens.

| Param | Type | Description |
| --- | --- | --- |
| s | <code>string</code> | The string to convert by replacing special characters with dash (-) |
| s | <code>string</code> | The string to convert. |

## Author

Expand Down
49 changes: 41 additions & 8 deletions slug.test.js → __tests__/file-easy.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { slug, setDefaultExtension, saveDocument } = require('./index'); // Adjust path if necessary
const { slug, setDefaultExtension, saveDocument } = require('../index'); // Adjust path if necessary
const fs = require('fs');
const path = require('path');

Expand Down Expand Up @@ -30,23 +30,56 @@ describe('slug', () => {
});




describe('setDefaultExtension', () => {
it('should throw an error for an extension without a leading dot', () => {
const filename = 'example';
const invalidExtension = 'txt';
expect(() => setDefaultExtension(filename, invalidExtension)).toThrow('Invalid extension: must start with a dot and contain only alphanumeric characters.');
});

it('should return filename with default extension', () => {

expect(setDefaultExtension('myfile', '.md')).toBe('myfile.md');
it('should throw an error for an extension with invalid characters', () => {
const filename = 'example';
const invalidExtension = '.tx$t';
expect(() => setDefaultExtension(filename, invalidExtension)).toThrow('Invalid extension: must start with a dot and contain only alphanumeric characters.');
});

it('should not throw an error for a valid extension', () => {
const filename = 'example';
const validExtension = '.txt';
expect(() => setDefaultExtension(filename, validExtension)).not.toThrow();
});

it('should return the filename with the default extension if no extension is present', () => {
const filename = 'example';
const extension = '.txt';
const result = setDefaultExtension(filename, extension);
expect(result).toBe('example.txt');
});

it('should return filename with existing extension', () => {
it('should return the original filename if it already has an extension', () => {
const filename = 'example.txt';
const extension = '.md';
const result = setDefaultExtension(filename, extension);
expect(result).toBe('example.txt');
});

expect(setDefaultExtension('myfile.txt', '.md')).toBe('myfile.txt');
it('should throw an error if filename is not a string', () => {
const invalidFilename = 12345;
const extension = '.txt';
expect(() => setDefaultExtension(invalidFilename, extension)).toThrow('Invalid input: `filename` must be a string.');
});

it('should throw an error if extension is not a string', () => {
const filename = 'example';
const invalidExtension = 12345;
expect(() => setDefaultExtension(filename, invalidExtension)).toThrow('Invalid input: `extension` must be a string.');
});

it('should throw an error if filename is empty', () => {
const emptyFilename = '';
const extension = '.txt';
expect(() => setDefaultExtension(emptyFilename, extension)).toThrow('Invalid input: `filename` cannot be empty.');
});
});


Expand Down
53 changes: 37 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ const path = require('path')
const fs = require('fs')


/**
* Append specified extension if needed.
*
* @param {string} filename the filename to check for an existing extension.
* @param {string} extension the extension to append if filename has no extension. It should start with a dot (e.g. `.txt`)
* @returns {string} filename with either existing or specified extension
*/
function setDefaultExtension(filename, extension) {
return (path.extname(filename)) ? filename : filename + extension;
}

/**
* Save content in a file using utf8 format.
*
Expand Down Expand Up @@ -47,6 +36,40 @@ function saveDocument(filename, content) {
fs.writeFileSync(filename, content, 'utf8');
}

/**
* Append specified extension if needed.
*
* @param {string} filename The filename to check for an existing extension.
* @param {string} extension The extension to append if filename has no extension. It should start with a dot (e.g. `.txt`)
* @returns {string} filename with either existing or specified extension
* @throws {TypeError} If filename or extension is not a string.
* @throws {Error} If extension is empty or doesn't start with a dot.
*/
function setDefaultExtension(filename, extension) {
// Validate inputs
const validExtensionPattern = /^\.[a-zA-Z0-9]+$/;
if (typeof filename !== 'string') {
throw new TypeError('Invalid input: `filename` must be a string.');
}
if (typeof extension !== 'string') {
throw new TypeError('Invalid input: `extension` must be a string.');
}
if (filename.trim() === '') {
throw new Error('Invalid input: `filename` cannot be empty.');
}

// Validate extension
if (!validExtensionPattern.test(extension)) {
throw new Error('Invalid extension: must start with a dot and contain only alphanumeric characters.');
}

// Return filename with existing extension, or append the specified extension
if (path.extname(filename)) {
return filename;
}

return `${filename}${extension}`;
}

/**
* Convert a string into an identifier.
Expand Down Expand Up @@ -83,10 +106,8 @@ function slug(s) {
return s;
}



module.exports = {
slug: slug,
setDefaultExtension: setDefaultExtension,
saveDocument: saveDocument
saveDocument,
setDefaultExtension,
slug,
}
60 changes: 60 additions & 0 deletions output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Functions

<dl>
<dt><a href="#saveDocument">saveDocument(filename, content)</a></dt>
<dd><p>Save content in a file using utf8 format.</p>
</dd>
<dt><a href="#setDefaultExtension">setDefaultExtension(filename, extension)</a> ⇒ <code>string</code></dt>
<dd><p>Append specified extension if needed.</p>
</dd>
<dt><a href="#slug">slug(s)</a> ⇒ <code>string</code></dt>
<dd><p>Convert a string into an identifier.</p>
</dd>
</dl>

<a name="saveDocument"></a>

## saveDocument(filename, content)
Save content in a file using utf8 format.

**Kind**: global function
**Throws**:

- <code>Error</code> If input is invalid or if there was an error during the save process.


| Param | Type | Description |
| --- | --- | --- |
| filename | <code>string</code> | The filename to create. It can also include a path ending with the filename. Path will be created if not exists. |
| content | <code>string</code> | The content to place in the file. |

<a name="setDefaultExtension"></a>

## setDefaultExtension(filename, extension) ⇒ <code>string</code>
Append specified extension if needed.

**Kind**: global function
**Returns**: <code>string</code> - filename with either existing or specified extension
**Throws**:

- <code>TypeError</code> If filename or extension is not a string.
- <code>Error</code> If extension is empty or doesn't start with a dot.


| Param | Type | Description |
| --- | --- | --- |
| filename | <code>string</code> | The filename to check for an existing extension. |
| extension | <code>string</code> | The extension to append if filename has no extension. It should start with a dot (e.g. `.txt`) |

<a name="slug"></a>

## slug(s) ⇒ <code>string</code>
Convert a string into an identifier.

**Kind**: global function
**Returns**: <code>string</code> - The identifier string.The following operations are performed on the string:1. Trim and convert to lower case.2. Remove diacritics (transliterate or remove non-ASCII characters).3. Replace spaces and special characters with hyphens, allowing underscores.4. Replace multiple hyphens with a single hyphen.5. Remove leading and trailing hyphens.

| Param | Type | Description |
| --- | --- | --- |
| s | <code>string</code> | The string to convert. |

Expand Down
78 changes: 0 additions & 78 deletions spec/file-easy-spec.js

This file was deleted.

11 changes: 0 additions & 11 deletions spec/support/jasmine.json

This file was deleted.

0 comments on commit 9443c1d

Please sign in to comment.