npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ffras4vnpm/totam-dolorem-impedit

v1.0.0

Published

[![npm version](https://img.shields.io/npm/v/@ffras4vnpm/totam-dolorem-impedit.svg)](https://www.npmjs.com/package/@ffras4vnpm/totam-dolorem-impedit) [![build status](https://travis-ci.org/adamreisnz/@ffras4vnpm/totam-dolorem-impedit.svg?branch=master)](h

Downloads

2

Maintainers

lank831011lank831011

Keywords

io-tsgetPrototypeOfwaapifastcloneyamlenderjson-schemacommandawaitprunebrowserlistsetImmediatereactscheme-validationSettsmixinsassignjapanesetc39ES2020persistentReactiveXcompile lesssharedarraybufferArray.prototype.flatMaptrimRightURLSearchParamsemojiformattingvalidatorbyteOffsettypesafefulloptimizerlook-upairbnbless compilerprotocol-bufferspusheventEmitterreact animationparentequalityPushbreakperformancewatchingfolderclassnamesstreamscryptoRxJSInt8Arraycolumnsutil.inspecttypeerrorastidlejsdifftrimLeftObjectencryptionframernameloggerbabelvaluesvaluetypebundlernopestylingObject.entriesiteratevisualhookformwatch0argvsequenceintrinsicreadablestreamansinested cssmomentzeroString.prototype.trimrapidtoStringTagdebugReactiveExtensionsdependenciespoint-freeflagsafecolournodejsObject.valueslessjoislicekeyargstappropertiesavaschemasearchjeststylesstringifydependency managerexecbddworkspace:*URLrgbobjectnativeterminalcode pointscreatepromisesECMAScript 2021fast-copy.envimmerquerystringisConcatSpreadablemochaWebSocketssomepathESfixed-width256xhrsyntaxtrimerror-handlingArray.prototype.flattenirqwaitreadpolyfillspringfunctionzodoperating-systemtslibstylesignalsAsyncIteratorlettesteruser-streamsfile systemuuidcodesUint8ClampedArraygetoptObject.isredactECMAScript 2017apolloprettywritemoduleslistenersgetdeepcopystructuredClonelesscssES2017xssReflect.getPrototypeOfextendECMAScript 3ES2019ES6arraybuffermergecontainstranspilerarktypethrottlelibphonenumbermobiledeep-clonephonetakeprefixfindLastIndexUnderscoreserializerlookes8utilitiesObject.definePropertylrudataViewdom-testing-librarytyped arrayfull-widthconcatgetter-0deep-copylengthbluebirdpnpm9json-schema-validatorguidTypedArraysharedbufferscomparecss lessreduxkeysstreams2flattenawesomesaucecollectiondeterministicstartergenericsstylesheethttpsortedstyled-componentsinstallerperformantdefinePropertyES2023bannercall-boundimmutableargparseautoprefixerinputreal-timeArray.prototype.containsramdajsdomminimalwatcherinterruptsArray.prototype.flatutilityconsumetapewhatwgbufferprivatePromisedatebabel-corehttpsStyleSheetimportplugincsshigher-orderECMAScript 2019traverseHyBihelpersfastifyqueryObject.keysdirectoryhandlersECMAScript 2016hasenumerablemetadataFloat64Arraydropescapestyleguidenegative zerosyntaxerrortacitES5ObservabletoolsbrowserslistflagsdatastructurestdlibindicatorES2022es5runtimesortshimtypedconcurrencyaccessibilityObservablesIteratorequalstatelessfind-upspinnersposebatchvares-shimsregexdeeppostcsswindowssideYAMLclassesvalidationFunction.prototype.nameoptimistcss nestingArray.prototype.findLastSymbol.toStringTages6compilerlockfilecrypttextreact-hook-formes2018configfindLastUint32ArrayES2018descriptionworkergroupByparserlinkpromiseincludesenvironmentrequestponyfilltimeES7pipecurlqsArray.prototype.includesWeakSetgetOwnPropertyDescriptorES2015everytypedarrayreuseuninstallmodulepureasyncCSSStyleDeclarationemitbootstrap lessconnectclientchromeES3fetches-shim APIreadableeslint-pluginvalidateFloat32ArrayArrayBuffereast-asian-widthpicomatchecmascripta11yassertefficientrmdironcefileclassnamelinuxpasswordrandompyyamlUint8Arraywidthextensionfast-deep-cloneform-validationlintlazysanitizei18npositiveexpressionobjArraymimetypesweaksetconfigurablejQueryJSONieexit-codecss variablequeuejsonschemapreprocessorwarningwordbreakjsxcjkpackage manageriteratorMicrosoftgradients cssMapeslintpluginObject.getPrototypeOfprotoboundsuperagentregular expressionstestdeletecore-jsspeedes2015TypeBoxbootstrap cssdotenvstringifiermakeES2016logassertsvariablesregular expressionSymbolES8stringthreetaskECMAScript 7filteriterationtypeofspechashStreampreserve-symlinksexitthroatcss-in-jspackagesnodedomcomputed-typeshardlinksauthviewauthenticationWeakMapchannelprocessloadingcallbindsetterisdeepcloneentriesartRxyupwrapoffseteventsfromBigInt64ArraygroupString.prototype.matchAllbindduplexcopykarmashellharmonyUint16ArraytddmatchesRFC-6455checksetPrototypeOfflatmake dirclass-validator$.extendsymbollanguageurlArray.prototype.filtercharacterargumenttestingwgethasOwnsignalgetintrinsicpropertymkdirless cssRegExp#flagstermsigtermtelephoneprototypejavascriptelectroncallbackarraystouchcolorECMAScript 2020limitmiddlewarepatchshamjasminecall-bindESnextspinnervestmatchAllparentswordwrapschemepostcss-plugincolumnframeworkcolorscliECMAScript 2015walkingchromiumeventDispatcherinstall_.extendglobsigintset3d__proto__lastfast-clonefinduppackage.jsonweakmapsameValueZeroresolve6to5rangeerrornumberarraytostringtaganimationtrimStarttoolkitdescriptorprivate datatypes@@toStringTagObject.fromEntriesinferenceformatshebangfsserializecloneArrayBuffer.prototype.slicerfc4122superstructregexpserializationfigletoptionfantasy-landtoArrayrobustArray.prototype.findLastIndexnamesaccessorpackageeslintconfigmonorepomulti-packageesinspectratecoerciblenpmconstwebtypedarraysdirECMAScript 2022mkdirpinternal slotcircularfullwidthapiquoteajvsymlinksdebuggercollection.es6react posedatapopmotionlimited

Readme

Replace in file

npm version build status coverage status github issues

A simple utility to quickly replace text in one or more files or globs. Works synchronously or asynchronously with either promises or callbacks. Make a single replacement or multiple replacements at once.

Index

Installation

# Using npm, installing to local project
npm i --save @ffras4vnpm/totam-dolorem-impedit

# Using npm, installing globally for global cli usage
npm i -g @ffras4vnpm/totam-dolorem-impedit

# Using yarn
yarn add @ffras4vnpm/totam-dolorem-impedit

Basic usage

//Load the library and specify options
const replace = require('@ffras4vnpm/totam-dolorem-impedit');
const options = {
  files: 'path/to/file',
  from: /foo/g,
  to: 'bar',
};

Asynchronous replacement with async/await

try {
  const results = await replace(options)
  console.log('Replacement results:', results);
}
catch (error) {
  console.error('Error occurred:', error);
}

Asynchronous replacement with promises

replace(options)
  .then(results => {
    console.log('Replacement results:', results);
  })
  .catch(error => {
    console.error('Error occurred:', error);
  });

Asynchronous replacement with callback

replace(options, (error, results) => {
  if (error) {
    return console.error('Error occurred:', error);
  }
  console.log('Replacement results:', results);
});

Synchronous replacement

try {
  const results = replace.sync(options);
  console.log('Replacement results:', results);
}
catch (error) {
  console.error('Error occurred:', error);
}

Return value

The return value of the library is an array of replacement results against each file that was processed. This includes files in which no replacements were made.

Each result contains the following values:

  • file: The path to the file that was processed
  • hasChanged: Flag to indicate if the file was changed or not
const results = replace.sync({
  files: 'path/to/files/*.html',
  from: /foo/g,
  to: 'bar',
});

console.log(results);

// [
//   {
//     file: 'path/to/files/file1.html',
//     hasChanged: true,
//   },
//   {
//     file: 'path/to/files/file2.html',
//     hasChanged: true,
//   },
//   {
//     file: 'path/to/files/file3.html',
//     hasChanged: false,
//   },
// ]

To get an array of changed files, simply map the results as follows:

const changedFiles = results
  .filter(result => result.hasChanged)
  .map(result => result.file);

Counting matches and replacements

By setting the countMatches configuration flag to true, the number of matches and replacements per file will be counted and present in the results array.

  • numMatches: Indicates the number of times a match was found in the file
  • numReplacements: Indicates the number of times a replacement was made in the file

Note that the number of matches can be higher than the number of replacements if a match and replacement are the same string.

const results = replace.sync({
  files: 'path/to/files/*.html',
  from: /foo/g,
  to: 'bar',
  countMatches: true,
});

console.log(results);

// [
//   {
//     file: 'path/to/files/file1.html',
//     hasChanged: true,
//     numMatches: 3,
//     numReplacements: 3,
//   },
//   {
//     file: 'path/to/files/file2.html',
//     hasChanged: true,
//     numMatches: 1,
//     numReplacements: 1,
//   },
//   {
//     file: 'path/to/files/file3.html',
//     hasChanged: false,
//     numMatches: 0,
//     numReplacements: 0,
//   },
// ]

Custom processor

For advanced usage where complex processing is needed it's possible to use a callback that will receive content as an argument and should return it processed.

const results = replace.sync({
  files: 'path/to/files/*.html',
  processor: (input) => input.replace(/foo/g, 'bar'),
});

The custom processor will receive the path of the file being processed as a second parameter:

const results = replace.sync({
  files: 'path/to/files/*.html',
  processor: (input, file) => input.replace(/foo/g, file),
});

Array of custom processors

Passing processor function also supports passing an array of functions that will be executed sequentially

function someProcessingA(input) {
  const chapters = input.split('###')
  chapters[1] = chapters[1].replace(/foo/g, 'bar')
  return chapters.join('###')
}

function someProcessingB(input) {
  return input.replace(/foo/g, 'bar')
}

const results = replace.sync({
  files: 'path/to/files/*.html',
  processor: [someProcessingA, someProcessingB],
});

Advanced usage

Replace a single file or glob

const options = {
  files: 'path/to/file',
};

Replace multiple files or globs

const options = {
  files: [
    'path/to/file',
    'path/to/other/file',
    'path/to/files/*.html',
    'another/**/*.path',
  ],
};

Replace first occurrence only

const options = {
  from: 'foo',
  to: 'bar',
};

Replace all occurrences

Please note that the value specified in the from parameter is passed straight to the native String replace method. As such, if you pass a string as the from parameter, it will only replace the first occurrence.

To replace multiple occurrences at once, you must use a regular expression for the from parameter with the global flag enabled, e.g. /foo/g.

const options = {
  from: /foo/g,
  to: 'bar',
};

Multiple values with the same replacement

These will be replaced sequentially.

const options = {
  from: [/foo/g, /baz/g],
  to: 'bar',
};

Multiple values with different replacements

These will be replaced sequentially.

const options = {
  from: [/foo/g, /baz/g],
  to: ['bar', 'bax'],
};

Custom regular expressions

Use the RegExp constructor to create any regular expression.

const str = 'foo';
const regex = new RegExp('^' + str + 'bar', 'i');
const options = {
  from: regex,
  to: 'bar',
};

Using callbacks for from

You can also specify a callback that returns a string or a regular expression. The callback receives the name of the file in which the replacement is being performed, thereby allowing the user to tailor the search string. The following example uses a callback to produce a search string dependent on the filename:

const options = {
  files: 'path/to/file',
  from: (file) => new RegExp(file, 'g'),
  to: 'bar',
};

Using callbacks for to

As the to parameter is passed to the native String replace method, you can also specify a callback. The following example uses a callback to convert matching strings to lowercase:

const options = {
  files: 'path/to/file',
  from: /SomePattern[A-Za-z-]+/g,
  to: (match) => match.toLowerCase(),
};

This callback provides for an extra argument above the String replace method, which is the name of the file in which the replacement is being performed. The following example replaces the matched string with the filename:

const options = {
  files: 'path/to/file',
  from: /SomePattern[A-Za-z-]+/g,
  to: (...args) => args.pop(),
};

Ignore a single file or glob

const options = {
  ignore: 'path/to/ignored/file',
};

Ignore multiple files or globs

const options = {
  ignore: [
    'path/to/ignored/file',
    'path/to/other/ignored_file',
    'path/to/ignored_files/*.html',
    'another/**/*.ignore',
  ],
};

Please note that there is an open issue with Glob that causes ignored patterns to be ignored when using a ./ prefix in your files glob. To work around this, simply remove the prefix, e.g. use **/* instead of ./**/*.

Allow empty/invalid paths

If set to true, empty or invalid paths will fail silently and no error will be thrown. For asynchronous replacement only. Defaults to false.

const options = {
  allowEmptyPaths: true,
};

Disable globs

You can disable globs if needed using this flag. Use this when you run into issues with file paths like files like //SERVER/share/file.txt. Defaults to false.

const options = {
  disableGlobs: true,
};

Specify glob configuration

Specify configuration passed to the glob call:

const options = {
  glob: {

    //Glob settings here (examples given below)
    dot: true, //To include file names starting with a dot
    windowsPathsNoEscape: true, //To fix paths on Windows OS when path.join() is used to create paths
  },
};

Please note that the setting nodir will always be passed as false.

Making replacements on network drives

To make replacements in files on network drives, you may need to specify the UNC path as the cwd config option. This will then be passed to glob and prefixed to your paths accordingly. See #56 for more details.

Specify character encoding

Use a different character encoding for reading/writing files. Defaults to utf-8.

const options = {
  encoding: 'utf8',
};

Dry run

To do a dry run without actually making replacements, for testing purposes. Defaults to false.

const options = {
  dry: true,
};

File system

@ffras4vnpm/totam-dolorem-impedit defaults to using require('fs') to provide file reading and write APIs. You can provide an fs object of your own to switch to a different file system, such as a mock file system for unit tests.

  • If using asynchronous APIs, the provided fs must provide readFile and writeFile methods
  • If using synchronous APIs, the provided fs must provide readFileSync and writeFileSync methods

Custom fs methods should have the same parameters and returned values as their built-in Node fs equivalents.

replaceInFile({
  from: 'a',
  fs: {
    readFile: (file, encoding, callback) => {
      console.log(`Reading ${file} with encoding ${encoding}...`);
      callback(null, 'fake file contents');
    },
    writeFile: (file, newContents, encoding, callback) => {
      console.log(`Writing ${file} with encoding ${encoding}: ${newContents}`);
      callback(null);
    },
  },
  to: 'b',
})

CLI usage

@ffras4vnpm/totam-dolorem-impedit from to some/file.js,some/**/glob.js
  [--configFile=replace-config.js]
  [--ignore=ignore/files.js,ignore/**/glob.js]
  [--encoding=utf-8]
  [--disableGlobs]
  [--isRegex]
  [--verbose]
  [--quiet]
  [--dry]

Multiple files or globs can be replaced by providing a comma separated list.

The flags --disableGlobs, --ignore and --encoding are supported in the CLI.

The setting allowEmptyPaths is not supported in the CLI as the replacement is synchronous, and this setting is only relevant for asynchronous replacement.

To list the changed files, use the --verbose flag. Success output can be suppressed by using the --quiet flag.

To do a dry run without making any actual changes, use --dry.

A regular expression may be used for the from parameter by specifying the --isRegex flag.

The from and to parameters, as well as the files list, can be omitted if you provide this information in a configuration file. You can provide a path to a configuration file (either Javascript or JSON) with the --configFile flag. This path will be resolved using Node’s built in path.resolve(), so you can pass in an absolute or relative path.

A note on using globs with the CLI

When using the CLI, the glob pattern is handled by the operating system. But if you specify the glob pattern in the configuration file, the package will use the glob module from the Node modules, and this can lead to different behaviour despite using the same pattern.

For example, the following will only look at top level files:

//config.js
module.exports = {
  from: /cat/g,
  to: 'dog',
};
@ffras4vnpm/totam-dolorem-impedit **  --configFile=config.js

However, this example is recursive:

//config.js
module.exports = {
  files: '**',
  from: /cat/g,
  to: 'dog',
};
@ffras4vnpm/totam-dolorem-impedit --configFile=config.js

If you want to do a recursive file search as an argument you must use:

@ffras4vnpm/totam-dolorem-impedit $(ls l {,**/}*)  --configFile=config.js

Version information

From version 3.0.0 onwards, replace in file requires Node 6 or higher. If you need support for Node 4 or 5, please use version 2.x.x.

From version 5.0.0 onwards, replace in file requires Node 8 or higher. If you need support for Node 6, please use version 4.x.x.

See the Changelog for more information.

License

(MIT License)

Copyright 2015-2023, Adam Reis, Co-founder at Hello Club and Frello