Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vfile

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vfile - npm Package Compare versions

Comparing version 4.2.0 to 4.2.1

lib/core.js

169

core.js
'use strict'
var path = require('path')
var replace = require('replace-ext')
var buffer = require('is-buffer')
module.exports = VFile
var own = {}.hasOwnProperty
var proto = VFile.prototype
// Order of setting (least specific to most), we need this because otherwise
// `{stem: 'a', path: '~/b.js'}` would throw, as a path is needed before a
// stem can be set.
var order = ['history', 'path', 'basename', 'stem', 'extname', 'dirname']
proto.toString = toString
// Access full path (`~/index.min.js`).
Object.defineProperty(proto, 'path', {get: getPath, set: setPath})
// Access parent path (`~`).
Object.defineProperty(proto, 'dirname', {get: getDirname, set: setDirname})
// Access basename (`index.min.js`).
Object.defineProperty(proto, 'basename', {get: getBasename, set: setBasename})
// Access extname (`.js`).
Object.defineProperty(proto, 'extname', {get: getExtname, set: setExtname})
// Access stem (`index.min`).
Object.defineProperty(proto, 'stem', {get: getStem, set: setStem})
// Construct a new file.
function VFile(options) {
var prop
var index
var length
if (!options) {
options = {}
} else if (typeof options === 'string' || buffer(options)) {
options = {contents: options}
} else if ('message' in options && 'messages' in options) {
return options
}
if (!(this instanceof VFile)) {
return new VFile(options)
}
this.data = {}
this.messages = []
this.history = []
this.cwd = process.cwd()
// Set path related properties in the correct order.
index = -1
length = order.length
while (++index < length) {
prop = order[index]
if (own.call(options, prop)) {
this[prop] = options[prop]
}
}
// Set non-path related properties.
for (prop in options) {
if (order.indexOf(prop) === -1) {
this[prop] = options[prop]
}
}
}
function getPath() {
return this.history[this.history.length - 1]
}
function setPath(path) {
assertNonEmpty(path, 'path')
if (path !== this.path) {
this.history.push(path)
}
}
function getDirname() {
return typeof this.path === 'string' ? path.dirname(this.path) : undefined
}
function setDirname(dirname) {
assertPath(this.path, 'dirname')
this.path = path.join(dirname || '', this.basename)
}
function getBasename() {
return typeof this.path === 'string' ? path.basename(this.path) : undefined
}
function setBasename(basename) {
assertNonEmpty(basename, 'basename')
assertPart(basename, 'basename')
this.path = path.join(this.dirname || '', basename)
}
function getExtname() {
return typeof this.path === 'string' ? path.extname(this.path) : undefined
}
function setExtname(extname) {
var ext = extname || ''
assertPart(ext, 'extname')
assertPath(this.path, 'extname')
if (ext) {
if (ext.charAt(0) !== '.') {
throw new Error('`extname` must start with `.`')
}
if (ext.indexOf('.', 1) !== -1) {
throw new Error('`extname` cannot contain multiple dots')
}
}
this.path = replace(this.path, ext)
}
function getStem() {
return typeof this.path === 'string'
? path.basename(this.path, this.extname)
: undefined
}
function setStem(stem) {
assertNonEmpty(stem, 'stem')
assertPart(stem, 'stem')
this.path = path.join(this.dirname || '', stem + (this.extname || ''))
}
// Get the value of the file.
function toString(encoding) {
var value = this.contents || ''
return buffer(value) ? value.toString(encoding) : String(value)
}
// Assert that `part` is not a path (i.e., does not contain `path.sep`).
function assertPart(part, name) {
if (part.indexOf(path.sep) !== -1) {
throw new Error(
'`' + name + '` cannot be a path: did not expect `' + path.sep + '`'
)
}
}
// Assert that `part` is not empty.
function assertNonEmpty(part, name) {
if (!part) {
throw new Error('`' + name + '` cannot be empty')
}
}
// Assert `path` exists.
function assertPath(path, name) {
if (!path) {
throw new Error('Setting `' + name + '` requires `path` to be set too')
}
}
module.exports = require('./lib/core')
'use strict'
var VMessage = require('vfile-message')
var VFile = require('./core.js')
module.exports = VFile
var proto = VFile.prototype
proto.message = message
proto.info = info
proto.fail = fail
// Create a message with `reason` at `position`.
// When an error is passed in as `reason`, copies the stack.
function message(reason, position, origin) {
var filePath = this.path
var message = new VMessage(reason, position, origin)
if (filePath) {
message.name = filePath + ':' + message.name
message.file = filePath
}
message.fatal = false
this.messages.push(message)
return message
}
// Fail: creates a vmessage, associates it with the file, and throws it.
function fail() {
var message = this.message.apply(this, arguments)
message.fatal = true
throw message
}
// Info: creates a vmessage, associates it with the file, and marks the fatality
// as null.
function info() {
var message = this.message.apply(this, arguments)
message.fatal = null
return message
}
module.exports = require('./lib')
{
"name": "vfile",
"version": "4.2.0",
"version": "4.2.1",
"description": "Virtual file format for text processing",

@@ -35,6 +35,15 @@ "license": "MIT",

"types": "types/index.d.ts",
"browser": {
"./lib/minpath.js": "./lib/minpath.browser.js",
"./lib/minproc.js": "./lib/minproc.browser.js"
},
"react-native": {
"./lib/minpath.js": "./lib/minpath.browser.js",
"./lib/minproc.js": "./lib/minproc.browser.js"
},
"files": [
"lib/",
"types/index.d.ts",
"core.js",
"index.js"
"index.js",
"core.js"
],

@@ -44,3 +53,2 @@ "dependencies": {

"is-buffer": "^2.0.0",
"replace-ext": "1.0.0",
"unist-util-stringify-position": "^2.0.0",

@@ -50,16 +58,16 @@ "vfile-message": "^2.0.0"

"devDependencies": {
"browserify": "^16.0.0",
"dtslint": "^3.0.0",
"browserify": "^17.0.0",
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^8.0.0",
"remark-preset-wooorm": "^7.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^2.0.0",
"xo": "^0.32.0"
"tinyify": "^3.0.0",
"xo": "^0.35.0"
},
"scripts": {
"format": "remark . -qfo && prettier . --write && xo --fix",
"build-bundle": "browserify . -s VFile > vfile.js",
"build-mangle": "browserify . -s VFile -p tinyify > vfile.min.js",
"build-bundle": "browserify . -s VFile -o vfile.js",
"build-mangle": "browserify . -s VFile -o vfile.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",

@@ -93,4 +101,8 @@ "test-api": "node test",

"rules": {
"unicorn/explicit-length-check": "off",
"unicorn/prefer-includes": "off",
"unicorn/prefer-reflect-apply": "off"
"unicorn/prefer-reflect-apply": "off",
"unicorn/prefer-number-properties": "off",
"max-depth": "off",
"complexity": "off"
}

@@ -102,6 +114,4 @@ },

[
"toc",
{
"heading": "contents"
}
"lint-no-html",
false
]

@@ -108,0 +118,0 @@ ]

@@ -1,4 +0,5 @@

# [![vfile][]][unified]
<h1>
<img src="https://raw.githubusercontent.com/vfile/vfile/7e1e6a6/logo.svg?sanitize=true" alt="vfile" width="400" />
</h1>
[![GitHub CI][github-ci-badge]][github-ci]
[![Build][build-badge]][build]

@@ -12,91 +13,26 @@ [![Coverage][coverage-badge]][coverage]

**vfile** is a virtual file format part of the [unified][] [collective][].
**vfile** is a small and browser friendly virtual file format that tracks
metadata (such as a file’s `path` and `contents`) and [messages][].
## Intro
It was made specifically for **[unified][]** and generally for the common task
of parsing, transforming, and serializing data, where `vfile` handles everything
about the document being compiled.
This is useful for example when building linters, compilers, static site
generators, or other build tools.
**vfile** is part of the [unified collective][site].
**vfile** is a virtual file format used by [**unified**][unified], a text
processing umbrella (it powers [**retext**][retext] for natural language,
[**remark**][remark] for markdown, and [**rehype**][rehype] for HTML).
Each processors that parse, transform, and compile text, and need a virtual
representation of files and a place to store [messages][] about them.
Plus, they work in the browser.
**vfile** provides these requirements at a small size.
* for updates, see [Twitter][]
* for more about us, see [`unifiedjs.com`][site]
* for questions, see [Discussions][chat]
* to help, see [contribute][] or [sponsor][] below
* Visit [`unifiedjs.com`][website] and see its [learn][] section for an
overview
* Read [unified][]’s readme for a technical intro
* Follow us on [Medium][] and [Twitter][] to see what we’re up to
* Check out [Contribute][] below to find out how to help out
> **vfile** is different from the excellent [`vinyl`][vinyl] in that it has
> a smaller API, a smaller size, and focuses on [messages][].
vfile can be used anywhere where files need a lightweight representation.
For example, it’s used in:
* [`documentation`](https://github.com/documentationjs/documentation)
— The documentation system for modern JavaScript
* [`awoo`](https://github.com/awoojs/awoo)
— Declarative small site generator
* [`geojsonhint`](https://github.com/mapbox/geojsonhint)
— Complete, fast, standards-based validation for geojson
## Sponsors
<!--lint ignore no-html-->
<table>
<tr valign="top">
<td width="33.33%" align="center" colspan="2">
<a href="https://www.gatsbyjs.org">Gatsby</a><br>🥇<br><br>
<a href="https://www.gatsbyjs.org"><img src="https://avatars1.githubusercontent.com/u/12551863?s=900&v=4"></a>
</td>
<td width="33.33%" align="center" colspan="2">
<a href="https://vercel.com">Vercel</a><br>🥇<br><br>
<!--OC has a sharper image-->
<a href="https://vercel.com"><img src="https://images.opencollective.com/vercel/d8a5bee/logo/512.png"></a>
</td>
<td width="33.33%" align="center" colspan="2">
<a href="https://www.netlify.com">Netlify</a><br><br><br>
<!--OC has a sharper image-->
<a href="https://www.netlify.com"><img src="https://images.opencollective.com/netlify/4087de2/logo/512.png"></a>
</td>
</tr>
<tr valign="top">
<td width="16.67%" align="center">
<a href="https://www.holloway.com">Holloway</a><br><br><br>
<a href="https://www.holloway.com"><img src="https://avatars1.githubusercontent.com/u/35904294?s=300&v=4"></a>
</td>
<td width="16.67%" align="center">
<a href="https://themeisle.com">ThemeIsle</a><br>🥉<br><br>
<a href="https://themeisle.com"><img src="https://twitter-avatar.now.sh/themeisle"></a>
</td>
<td width="16.67%" align="center">
<a href="https://boostio.co">BoostIO</a><br>🥉<br><br>
<a href="https://boostio.co"><img src="https://avatars1.githubusercontent.com/u/13612118?s=300&v=4"></a>
</td>
<td width="16.67%" align="center">
<a href="https://expo.io">Expo</a><br>🥉<br><br>
<a href="https://expo.io"><img src="https://avatars1.githubusercontent.com/u/12504344?s=300&v=4"></a>
</td>
<td width="50%" align="center" colspan="2">
<br><br><br><br>
<a href="https://opencollective.com/unified"><strong>You?</strong></a>
</td>
</tr>
</table>
## Install
[npm][]:
```sh
npm install vfile
```
## Contents
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`VFile([options])`](#vfileoptions)
* [`VFile(options?)`](#vfileoptions)
* [`vfile.contents`](#vfilecontents)

@@ -112,12 +48,21 @@ * [`vfile.cwd`](#vfilecwd)

* [`vfile.data`](#vfiledata)
* [`VFile#toString([encoding])`](#vfiletostringencoding)
* [`VFile#toString(encoding?)`](#vfiletostringencoding)
* [`VFile#message(reason[, position][, origin])`](#vfilemessagereason-position-origin)
* [`VFile#info(reason[, position][, origin])`](#vfileinforeason-position-origin)
* [`VFile#fail(reason[, position][, origin])`](#vfilefailreason-position-origin)
* [Utilities](#utilities)
* [List of utilities](#list-of-utilities)
* [Reporters](#reporters)
* [Contribute](#contribute)
* [Sponsor](#sponsor)
* [Acknowledgments](#acknowledgments)
* [License](#license)
## Install
[npm][]:
```sh
npm install vfile
```
## Use

@@ -167,3 +112,3 @@

### `VFile([options])`
### `VFile(options?)`

@@ -241,6 +186,7 @@ Create a new virtual file.

### `VFile#toString([encoding])`
### `VFile#toString(encoding?)`
Convert contents of `vfile` to string.
If `contents` is a buffer, `encoding` is used to stringify buffers (default:
When `contents` is a [`Buffer`][buffer], `encoding` is a
[character encoding][encoding] to understand `doc` as (`string`, default:
`'utf8'`).

@@ -278,6 +224,8 @@

## Utilities
<a name="utilities"></a>
## List of utilities
The following list of projects includes tools for working with virtual files.
See **[unist][]** for projects working with nodes.
See **[unist][]** for projects that work with nodes.

@@ -347,2 +295,47 @@ * [`convert-vinyl-to-vfile`](https://github.com/dustinspecker/convert-vinyl-to-vfile)

## Sponsor
Support this effort and give back by sponsoring on [OpenCollective][collective]!
<table>
<tr valign="middle">
<td width="20%" align="center" colspan="2">
<a href="https://www.gatsbyjs.org">Gatsby</a> 🥇<br><br>
<a href="https://www.gatsbyjs.org"><img src="https://avatars1.githubusercontent.com/u/12551863?s=256&v=4" width="128"></a>
</td>
<td width="20%" align="center" colspan="2">
<a href="https://vercel.com">Vercel</a> 🥇<br><br>
<a href="https://vercel.com"><img src="https://avatars1.githubusercontent.com/u/14985020?s=256&v=4" width="128"></a>
</td>
<td width="20%" align="center" colspan="2">
<a href="https://www.netlify.com">Netlify</a><br><br>
<!--OC has a sharper image-->
<a href="https://www.netlify.com"><img src="https://images.opencollective.com/netlify/4087de2/logo/256.png" width="128"></a>
</td>
<td width="10%" align="center">
<a href="https://www.holloway.com">Holloway</a><br><br>
<a href="https://www.holloway.com"><img src="https://avatars1.githubusercontent.com/u/35904294?s=128&v=4" width="64"></a>
</td>
<td width="10%" align="center">
<a href="https://themeisle.com">ThemeIsle</a><br><br>
<a href="https://themeisle.com"><img src="https://avatars1.githubusercontent.com/u/58979018?s=128&v=4" width="64"></a>
</td>
<td width="10%" align="center">
<a href="https://boosthub.io">Boost Hub</a><br><br>
<a href="https://boosthub.io"><img src="https://images.opencollective.com/boosthub/6318083/logo/128.png" width="64"></a>
</td>
<td width="10%" align="center">
<a href="https://expo.io">Expo</a><br><br>
<a href="https://expo.io"><img src="https://avatars1.githubusercontent.com/u/12504344?s=128&v=4" width="64"></a>
</td>
</tr>
<tr valign="middle">
<td width="100%" align="center" colspan="10">
<br>
<a href="https://opencollective.com/unified"><strong>You?</strong></a>
<br><br>
</td>
</tr>
</table>
## Acknowledgments

@@ -371,10 +364,6 @@

[github-ci-badge]: https://github.com/vfile/vfile/workflows/CI/badge.svg
[build-badge]: https://github.com/vfile/vfile/workflows/main/badge.svg
[github-ci]: https://github.com/vfile/vfile/actions
[build]: https://github.com/vfile/vfile/actions
[build-badge]: https://img.shields.io/travis/vfile/vfile.svg
[build]: https://travis-ci.org/vfile/vfile
[coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile.svg

@@ -398,5 +387,5 @@

[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://spectrum.chat/unified/vfile
[chat]: https://github.com/vfile/vfile/discussions

@@ -417,14 +406,14 @@ [npm]: https://docs.npmjs.com/cli/install

[vfile]: https://raw.githubusercontent.com/vfile/vfile/7e1e6a6/logo.svg?sanitize=true
[unified]: https://github.com/unifiedjs/unified
[retext]: https://github.com/retextjs/retext
[vinyl]: https://github.com/gulpjs/vinyl
[remark]: https://github.com/remarkjs/remark
[site]: https://unifiedjs.com
[rehype]: https://github.com/rehypejs/rehype
[twitter]: https://twitter.com/unifiedjs
[vinyl]: https://github.com/gulpjs/vinyl
[contribute]: #contribute
[sponsor]: #sponsor
[unist]: https://github.com/syntax-tree/unist#list-of-utilities

@@ -440,12 +429,6 @@

[website]: https://unifiedjs.com
[learn]: https://unifiedjs.com/learn/
[contribute]: #contribute
[ideas]: https://github.com/vfile/ideas
[medium]: https://medium.com/unifiedjs
[encoding]: https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings
[twitter]: https://twitter.com/unifiedjs
[buffer]: https://nodejs.org/api/buffer.html
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc