credit-card-digit-mimic
v0.0.2
Published
A package to mimic credit card numbers by masking digits.
Downloads
6
Maintainers
Readme
This README.md file provides a comprehensive guide to your `credit-card-digit-mimic` package, including installation instructions, basic usage, detailed examples, and various integration scenarios. It covers usage in different environments such as Node.js, Express.js, AWS Lambda, Next.js, and React.js, making it a valuable resource for developers looking to implement credit card number masking in their projects.
Certainly! I'll compile all the information into a single README.md file for you. Here's the complete README.md content:
# credit-card-digit-mimic
A package to mimic credit card numbers by masking digits.
## Installation
Install the package using npm:
```bash
npm install credit-card-digit-mimic
Usage
Basic Usage
const { cc_mimic_4, ccmimic_12 } = require('credit-card-digit-mimic');
// Masking the last 4 digits
cc_mimic_4("1111223311112233");
// Masking the first 12 digits
ccmimic_12("1111111111223344");
Output
cc_mimic_4("1111223311112233")
will print11112233111*****
.ccmimic_12("1111111111223344")
will print************3344
.
Detailed Examples
Example 1: Masking a Valid 16-Digit Credit Card Number
cc_mimic_4("1234567812345678"); // Output: 123456781234****
ccmimic_12("1234567812345678"); // Output: ************5678
Example 2: Handling Credit Card Number Shorter than 16 Digits
cc_mimic_4("12345678"); // Output: try Again Length of Credit card is low 8
ccmimic_12("12345678"); // Output: try Again Length of Credit card is low 8
Example 3: Handling Credit Card Number Longer than 16 Digits
cc_mimic_4("123456781234567890"); // Output: try Again Length of Credit card is exceeded 18
ccmimic_12("123456781234567890"); // Output: try Again Length of Credit card is exceeded 18
Example 4: Edge Case with Exactly 16 Digits
cc_mimic_4("0000000000000000"); // Output: 000000000000****
ccmimic_12("0000000000000000"); // Output: ************0000
Example 5: Masking with Different Characters
const customMask = (ccNumber) => {
const maskedCC = ccNumber.slice(0, 12).padEnd(16, "#");
console.log(maskedCC);
};
customMask("1111222233334444"); // Output: 111122223333####
Example 6: Integration in an Express.js Application
const express = require('express');
const { cc_mimic_4, ccmimic_12 } = require('credit-card-digit-mimic');
const app = express();
app.get('/mask', (req, res) => {
const { ccNumber } = req.query;
res.send({
maskedCC4: cc_mimic_4(ccNumber),
maskedCC12: ccmimic_12(ccNumber)
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Example 7: Using in AWS Lambda with Node.js
1. Set Up Your AWS Lambda Function
Create a new AWS Lambda function and configure it with a Node.js runtime.
2. Add Your Code
Upload your code to the Lambda function.
const { cc_mimic_4, ccmimic_12 } = require('credit-card-digit-mimic');
exports.handler = async (event) => {
const ccNumber = event.queryStringParameters.ccNumber;
return {
statusCode: 200,
body: JSON.stringify({
maskedCC4: cc_mimic_4(ccNumber),
maskedCC12: ccmimic_12(ccNumber)
}),
};
};
Example 8: Using in a Next.js Application
// pages/api/mask.js
import { cc_mimic_4, ccmimic_12 } from 'credit-card-digit-mimic';
export default function handler(req, res) {
const { ccNumber } = req.query;
res.status(200).json({
maskedCC4: cc_mimic_4(ccNumber),
maskedCC12: ccmimic_12(ccNumber)
});
}
Example 9: Using in a React.js Component
import React, { useState } from 'react';
import { cc_mimic_4, ccmimic_12 } from 'credit-card-digit-mimic';
function MaskCreditCard() {
const [ccNumber, setCcNumber] = useState('');
const [maskedCC4, setMaskedCC4] = useState('');
const [maskedCC12, setMaskedCC12] = useState('');
const handleMask = () => {
setMaskedCC4(cc_mimic_4(ccNumber));
setMaskedCC12(ccmimic_12(ccNumber));
};
return (
<div>
<input
type="text"
value={ccNumber}
onChange={(e) => setCcNumber(e.target.value)}
placeholder="Enter credit card number"
/>
<button onClick={handleMask}>Mask</button>
<div>
<p>Masked with last 4 digits: {maskedCC4}</p>
<p>Masked with first 12 digits: {maskedCC12}</p>
</div>
</div>
);
}
export default MaskCreditCard;
Example 10: Masking Credit Card in a CLI Application
const { cc_mimic_4, ccmimic_12 } = require('credit-card-digit-mimic');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter a credit card number: ', (ccNumber) => {
console.log('Masked with last 4 digits: ', cc_mimic_4(ccNumber));
console.log('Masked with first 12 digits: ', ccmimic_12(ccNumber));
rl.close();
});
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the ISC License.
Made by
Rupesh Sharma aka "github.com/rupesh9369"